如何仅更改工具栏中的后退箭头?

时间:2018-03-07 13:09:03

标签: android android-layout android-relativelayout

我正在使用ContentProvider

我已设法使用getSupportedActionBar().setTitle("This Phone");

将标题颜色更改为白色

如何将后箭头键颜色更改为白色?

6 个答案:

答案 0 :(得分:2)

请查看下面对我有用的kotlin函数。

private fun setToolbarProperties() {
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    supportActionBar?.title = "" //If you don't want to set title
    val upArrow = resources.getDrawable(R.drawable.ic_action_back,theme);
    supportActionBar?.setHomeAsUpIndicator(upArrow);
}

下面是工具栏xml代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    >

</androidx.appcompat.widget.Toolbar>

答案 1 :(得分:1)

您可以在活动中尝试: - ( setSupportActionBar(工具栏)之后)

  Drawable backArrow = context.getResources().getDrawable(R.drawable.your_back_button);
  backArrow.setColorFilter(getResources().getColor(R.color.yourcolor), PorterDuff.Mode.SRC_ATOP);
  getSupportActionBar().setHomeAsUpIndicator(backArrow);

答案 2 :(得分:1)

Joy Dey。
尝试在styles.xml中将您的应用主题更改为“Theme.AppCompat.Light.NoActionBar”。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style> 

答案 3 :(得分:1)

尝试像这样在styles.xml中更改图标本身

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

            <item name="android:homeAsUpIndicator">@drawable/ic_new_icon</item>
    </style>

答案 4 :(得分:0)

尝试使用以下代码:

  

从drawable中获取图像,然后设置颜色

final Drawable backArrow = getResources().getDrawable(R.drawable.back_img);
backArrow .setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow );

如果您想更改ToolBar标题&amp;的颜色后退按钮图像为白色按样式

参考: https://stackoverflow.com/a/33339983/8448886

答案 5 :(得分:0)

在style.xml中添加以下代码

<style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
  <!-- Customize color of navigation drawer icon and back arrow --> 
  <item name="colorControlNormal">@color/toolbar_color_control_normal</item>
</style>

并在app:theme="@style/ToolbarStyle"

中添加此属性toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ToolbarStyle" >
</android.support.v7.widget.Toolbar>
相关问题