更改Android ActionBar上的自定义向上/后退按钮图标

Dav*_*vid 4 android android-actionbar up-button android-styles

我试图使用具有不同颜色的自定义向上/向后按钮图标,因为我一直无法弄清更改默认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>
Run Code Online (Sandbox Code Playgroud)

在我的活动的onCreate中,我包括了以下内容:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.upbutton);
Run Code Online (Sandbox Code Playgroud)

在onOptionsItemSelected中,我这样做:

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

请谁能帮我弄清楚我做错了什么,或者我错过了什么?提前致谢。

Sil*_*ria 6

这是我的toolbar.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>
Run Code Online (Sandbox Code Playgroud)

以及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>
Run Code Online (Sandbox Code Playgroud)

您仍然需要执行以下操作:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.upbutton);
Run Code Online (Sandbox Code Playgroud)