【问题标题】:Change custom Up/Back Button Icon on Android ActionBar更改 Android ActionBar 上的自定义上/后退按钮图标
【发布时间】:2017-11-10 03:49:38
【问题描述】:

我正在尝试使用具有不同颜色的自定义上/后退按钮图标,因为我无法找出更改默认 android 上/后退按钮颜色的直接方法。

我下载了一个 128x128 的后退按钮图标 (PNG) 并将其更改为我想要的颜色,并将其放在我的可绘制文件夹中。但我仍然无法让它显示出来。

到目前为止,这是我尝试过的,

在我的 Styles.xml 中,我包含了这个:

<style name="customStyle" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/customActionBarStyle</item>
        <item name="android:textColorPrimary">@color/titletextcolor</item>
    </style>

    <style name="customActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/custom</item>
        <item name="android:homeAsUpIndicator">@drawable/upbutton</item>
    </style>

在我的 Activity 的 onCreate 中,我包含了这个:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.upbutton);

在 onOptionsItemSelected 中,我这样做了:

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
        }
        return true;
    }

请任何人都可以帮助找出我做错了什么,或者我错过了什么?提前致谢。

【问题讨论】:

  • 您是否为 ActionBar 设置了自定义样式?
  • 试试getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); etSupportActionBar().setHomeAsUpIndicator(R.drawable.upbutton);
  • 是的,我在清单文件中设置了自定义样式。 (nhoxbypass)
  • 我是否使用所有 4 行代码? (Shobhit Puri)
  • 对。尝试设置所有 4

标签: android android-actionbar android-styles up-button


【解决方案1】:

这是我的工具栏.xml

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        >

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <TextView
                    android:id="@+id/activity_title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    android:gravity="center"
                    tools:text="[ACTIVITY TITLE]"/>

        </LinearLayout>

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

以及styles.xml中的条目

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/colorDarkBlue</item>
    <item name="android:homeAsUpIndicator">@drawable/upbutton</item>
</style>

你还要做:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.upbutton);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2012-06-16
    • 2010-11-23
    相关资源
    最近更新 更多