更改Android ActionBar上的自定义向上/后退按钮图标 [英] Change custom Up/Back Button Icon on Android ActionBar

查看:86
本文介绍了更改Android ActionBar上的自定义向上/后退按钮图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用其他颜色的自定义向上/向后按钮图标,因为我一直无法找出改变默认android向上/向后按钮颜色的直接方法.

I'm trying to use a custom up/back button icon with a different color since I haven't been able to figure out a straight forward way of changing the color of the default android up/back button.

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

I downloaded a 128x128 back button icon (PNG) and changed it to the color I want, also placed it in my drawable folder. But i have not been able to get it to display, still.

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

Here's what I tried so far,

在我的Styles.xml中,包括了以下内容:

In my Styles.xml, I included this:

<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>

在我的活动的onCreate中,我添加了以下内容:

In my Activity's onCreate, I included this:

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

在onOptionsItemSelected中,我做到了:

And in the onOptionsItemSelected, I did this:

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

请问有人可以帮我弄清楚我做错了什么,或者我错过了什么?预先感谢.

Please can anyone help figure out what i'm doing wrong, or what I'm missing? Thanks in advance.

推荐答案

这是我的toolbar.xml

Here is my 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>

以及styles.xml中的条目

and the entry in the 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);

这篇关于更改Android ActionBar上的自定义向上/后退按钮图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆