Change the actionbar homeAsUpIndicator Programamti

2019-01-24 16:37发布

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);
}

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

11条回答
甜甜的少女心
2楼-- · 2019-01-24 16:38

You can achieve this in an easier way. Try to can change the homeAsUpIndicator attribute of actionBarStyle in your theme.xml and styles.xml.

If you want some padding, just add some white space in your image.

查看更多
\"骚年 ilove
3楼-- · 2019-01-24 16:38
this.getSupportActionBar().setDisplayUseLogoEnabled(true);
this.getSupportActionBar().setLogo(R.drawable.about_selected);

Also you can define the logo in manifest in attribute android:logo of and tags and set in theme that you want to use logo instead of app icon in the action bar.

查看更多
爷的心禁止访问
4楼-- · 2019-01-24 16:39

The question was to change dynamically the Up Home Indicator, although this answer was accepting and it is about Themes and Styles. I found a way to do this programmatically, according to Adneal's answer which gives me the clue and specially the right way to do. I used the below snippet code and it works well on (tested) devices with APIs mentioned here.

For lower APIs, I use R.id.up which is not available on higher API. That's why, I retrieve this id by a little workaround which is getting the parent of home button (android.R.id.home) and its first child (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));
} 

Note: If you don't use the SDK condition, you will get some NullPointerException.

查看更多
不美不萌又怎样
5楼-- · 2019-01-24 16:40

You can try this:

this.getSupportActionBar().setHomeAsUpIndicator( R.drawable.actionbar_indicator ); //for ActionBarCompat
this.getActionBar().setHomeAsUpIndicator( R.drawable.actionbar_indicator ); //for default actionbar for post 3.0 devices

If you need change the position of the icon, you must create a drawable file containing a "layer-list" like this:

actionbar_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/indicator"
        android:right="5dp"
        android:left="10dp" />
</layer-list>
查看更多
叼着烟拽天下
6楼-- · 2019-01-24 16:41

All you need to do is to use this line of code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This will change the icon with the up indicator. To disable it later, just call this function again and pass false as the param.

查看更多
来,给爷笑一个
7楼-- · 2019-01-24 16:44

If someone uses the library support-v7 appcompat, you can directly call this method:

  • getSupportActionBar().setHomeAsUpIndicator(int redId)

In other case you can use this solution:

查看更多
登录 后发表回答