6

我使用以下技巧以编程方式更改 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);
}

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

4

12 回答 12

18

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

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

在android清单中,我将活动主题设为如上。

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

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

于 2013-07-11T12:27:15.610 回答
11

问题是动态地改变Up Home Indicator,尽管这个答案是可以接受的并且它是关于Themesand Styles。根据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));
} 

注意:如果你不使用 SDK 条件,你会得到一些NullPointerException.

于 2014-05-07T16:08:53.557 回答
5

API 18 有新方法ActionBar.setHomeAsUpIndicator()- 不幸的是,目前支持库不支持这些方法

http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(android.graphics.drawable.Drawable)

编辑:现在支持库 http://developer.android.com/reference/android/support/v7/app/ActionBar.html#setHomeAsUpIndicator(android.graphics.drawable.Drawable)支持这些

于 2013-09-18T10:51:09.650 回答
3

您需要做的就是使用这行代码:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

这将更改带有向上指示器的图标。要稍后禁用它,只需再次调用此函数并false作为参数传递。

于 2013-07-11T06:49:43.773 回答
2

通过检查的解决方案Resources.getSystem()不适用于所有设备,更改它的更好解决方案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 的方法,请参阅文档

于 2013-09-23T09:30:29.897 回答
1

您可以以更简单的方式实现这一目标。尝试可以在你的 theme.xml 和 styles.xml 中更改 actionBarStyle 的 homeAsUpIndicator 属性。

如果您想要一些填充,只需在图像中添加一些空白。

于 2013-07-11T05:54:30.107 回答
1

你可以试试这个:

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

如果您需要更改图标的位置,您必须创建一个包含“图层列表”的可绘制文件,如下所示:

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>
于 2015-03-10T15:27:18.277 回答
0

使用getActionBar().setCustomView(int yourView);是因为 ActionBar 没有办法改变 homeUp 图标!

于 2013-07-11T05:54:00.133 回答
0

添加到 Fllo 答案以编程方式更改操作栏 homeAsUpIndicator

我能够在 Android 4+ 上使用此 hack,但无法理解为什么在搜索小部件展开时 up/home 指示器恢复为默认值。查看视图层次结构,发现操作栏的向上/主页指示器 + 图标部分有 2 个实现,当然第一个是在搜索小部件未展开时的实现。所以这是我用来解决这个问题的代码,并在两种情况下都改变了向上/主页指示器。

    mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // https://stackoverflow.com/questions/17585892/change-the-actionbar-homeasupindicator-programamtically
            int actionBarId = getResources().getIdentifier("android:id/action_bar", null, null);
            View view = getActivity().getWindow().getDecorView().findViewById(actionBarId);
            if (view == null
                    || !(view instanceof ViewGroup)) {
                return true;
            }
            final ViewGroup actionBarView = (ViewGroup)view;

            // The second home view is only inflated after 
            // setOnActionExpandListener() is first called
            actionBarView.post(new Runnable() {
                @Override
                public void run() {
                    //The 2 ActionBarView$HomeView views are always children of the same view group
                    //However, they are not always children of the ActionBarView itself
                    //(depends on OS version)
                    int upId = getResources().getIdentifier("android:id/up", null, null);
                    View upView = actionBarView.findViewById(upId);
                    ViewParent viewParent = upView.getParent();
                    if (viewParent == null) {
                        return;
                    }
                    viewParent = viewParent.getParent();
                    if (viewParent == null
                            || !(viewParent instanceof ViewGroup)) {
                        return;
                    }

                    ViewGroup viewGroup = (ViewGroup) viewParent;
                    int childCount = viewGroup.getChildCount();
                    for (int i = 0; i < childCount; i++) {
                        View childView = viewGroup.getChildAt(i);
                        if (childView instanceof ViewGroup) {
                            ViewGroup homeView = (ViewGroup) childView;
                            upView = homeView.findViewById(upId);
                            if (upView != null
                                    && upView instanceof ImageView) {
                                Drawable upDrawable = getResources().getDrawable(R.drawable.ic_ab_back_holo_dark_am);
                                upDrawable.setColorFilter(accentColorInt, PorterDuff.Mode.MULTIPLY);
                                ((ImageView) upView).setImageDrawable(upDrawable);
                            }
                        }
                    }
                }
            });
于 2014-09-10T19:40:56.347 回答
0

如果有人使用库 support-v7 appcompat,可以直接调用这个方法:

  • getSupportActionBar().setHomeAsUpIndicator(int redId)

在其他情况下,您可以使用此解决方案:

于 2015-03-24T15:00:44.683 回答
0

如果您将DrawerLayoutActionBarDrawerToggle一起使用,请查看此答案

于 2019-03-04T22:04:28.413 回答
-1
this.getSupportActionBar().setDisplayUseLogoEnabled(true);
this.getSupportActionBar().setLogo(R.drawable.about_selected);

您还可以在属性 android:logo 和标签的清单中定义徽标,并在主题中设置您要在操作栏中使用徽标而不是应用程序图标。

于 2013-07-11T06:42:18.247 回答