以编程方式更改操作栏homeAsUpIndicator

Yas*_*sir 6 android actionbarsherlock android-actionbar

我用下面的技巧以编程方式更改homeAsupIndicator.

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);
}
Run Code Online (Sandbox Code Playgroud)

但是,这是行不通的大部分新手机(HTC一,银河S3等).有没有一种方法可以跨设备统一更改.我需要它只在主屏幕上更改.其他屏幕将具有默认屏幕.所以不能使用styles.xml

Yas*_*sir 18

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

<style name="CustomActivityTheme" parent="AppTheme">
    <item name="android:homeAsUpIndicator">@drawable/custom_home_as_up_icon</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在android清单中我将活动主题如上所述.

<activity
        android:name="com.example.CustomActivity"
        android:theme="@style/CustomActivityTheme" >
</activity>
Run Code Online (Sandbox Code Playgroud)

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


fll*_*llo 11

现在的问题是改变动态Up Home Indicator,虽然这个答案被接受,它是关于ThemesStyles.根据Adneal的回答,我找到了一种以编程方式执行此操作的方法,该回答为我提供了线索,特别是正确的方法.我使用了下面的代码段代码,它适用于使用此处提到的API的(已测试)设备.

对于较低的API,我使用的R.id.up是较高API不可用的API.这就是为什么,我通过一个小的解决方法来获取这个id,它是获取home按钮(android.R.id.home)的父节点及其第一个子节点(android.R.id.up):

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    // get the parent view of home (app icon) imageview
    ViewGroup home = (ViewGroup) findViewById(android.R.id.home).getParent();
    // get the first child (up imageview)
    ( (ImageView) home.getChildAt(0) )
        // change the icon according to your needs
        .setImageResource(R.drawable.custom_icon_up));
} else {
    // get the up imageview directly with R.id.up
    ( (ImageView) findViewById(R.id.up) )
        .setImageResource(R.drawable.custom_icon_up));
} 
Run Code Online (Sandbox Code Playgroud)

注意:如果您不使用SDK条件,您将获得一些NullPointerException.