我知道如何change the homeAsUpIndicator in the styles xml file。问题是如何以编程的方式对其进行更改。
我之所以要这样做,是因为在一些视图中,我支持侧导航(滑动菜单)--按下up/back标题按钮,就会显示侧菜单。
在其他视图中,我支持自然的up/back botton。
因此,我想用不同的指示器图标来表示两种不同的逻辑--侧导航与上/后导航。
请不要争论这样做的动机。这就是给定的状态。谢谢。
发布于 2013-05-07 01:45:35
int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
ImageView up = (ImageView) findViewById(upId);
up.setImageResource(R.drawable.ic_drawer_indicator);
}
发布于 2013-09-23 17:29:21
@matthias的解决方案并不适用于所有设备,更改homeAsUpIndicator
的一个更好的解决方案是在样式中设置为@null,并以编程方式更改徽标资源。
下面是我来自style.xml的代码
<style name="Theme.HomeScreen" parent="AppBaseTheme">
<item name="displayOptions">showHome|useLogo</item>
<item name="homeAsUpIndicator">@null</item>
<item name="android:homeAsUpIndicator">@null</item>
</style>
在代码中,您可以使用setLogo()
方法更改徽标。
getSupportActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for ActionBarCompat
getActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for default actionbar for post 3.0 devices
还要注意,Android API 18
具有以编程方式编辑homeAsUpIndicator的方法refer documentation。
发布于 2016-02-29 07:25:10
这对我很有效
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_home_as_up)
https://stackoverflow.com/questions/16394755
复制相似问题