以编程方式更改操作栏homeAsUpIndicator [英] Change the actionbar homeAsUpIndicator Programmatically

查看:367
本文介绍了以编程方式更改操作栏homeAsUpIndicator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下技巧以编程方式更改了homeAsupIndicator。

I used the following hack to change the homeAsupIndicator programmatically.

int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
    ImageView up = (ImageView) findViewById(upId);
up.setImageResource(R.drawable.ic_action_bar_menu);
up.setPadding(0, 0, 20, 0);
}

但这不适用于大多数新手机(HTC One,Galaxy S3,等等)。有没有一种方法可以在设备之间进行统一更改。我只需要在主屏幕上进行更改。其他屏幕将具有默认屏幕。因此不能使用styles.xml

But this is not working on most new phones (HTC One, Galaxy S3, etc). Is there a way that can be changed uniformly across devices. I need it to be changed only on home screen. Other screens would have the default one. So cannot use the styles.xml

推荐答案

这是我为实现此行为所做的。我继承了基本主题,并创建了一个新主题以将其用作特定活动的主题。

This is what i did to acheive the behavior. I inherited the base theme and created a new theme to use it as a theme for the specific activity.

<style name="CustomActivityTheme" parent="AppTheme">
    <item name="android:homeAsUpIndicator">@drawable/custom_home_as_up_icon</item>
</style>

,并且在android清单中,我按照上述方式创建了活动主题。

and in the android manifest i made the activity theme as the above.

<activity
        android:name="com.example.CustomActivity"
        android:theme="@style/CustomActivityTheme" >
</activity>

效果很好。当我检查所有设备时将再次更新。感谢@faylon指出正确的方向

works great. Will update again when i check on all devices I have. Thanks @faylon for pointing in the right direction

这篇关于以编程方式更改操作栏homeAsUpIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆