【问题标题】:How to change only the back arrow in toolbar?如何仅更改工具栏中的后退箭头?
【发布时间】:2018-03-07 13:09:03
【问题描述】:

我正在使用getSupportedActionBar().setTitle("This Phone");

我已经设法使用Toolbar.setTitleTextColor(0xFFFFFFFF); 将标题颜色更改为白色

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

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    您可以在您的活动中尝试此操作:-(在 setSupportActionBar(toolbar) 之后)

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

      请看下面对我有用的 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>
      

      【讨论】:

        【解决方案3】:

        乔伊·戴伊。
        尝试在 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> 
        

        【讨论】:

          【解决方案4】:

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

              <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
          
                      <item name="android:homeAsUpIndicator">@drawable/ic_new_icon</item>
              </style>
          

          【讨论】:

            【解决方案5】:

            试试下面的代码:

            从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标题和后退按钮图像的颜色更改为白色

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

            【讨论】:

              【解决方案6】:

              在您的 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>
              

              并在您的toolbar 中添加此属性app:theme="@style/ToolbarStyle"

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

              【讨论】:

                猜你喜欢
                • 2016-04-07
                • 2017-07-09
                • 1970-01-01
                • 1970-01-01
                • 2015-12-02
                • 1970-01-01
                • 1970-01-01
                • 2019-04-19
                • 1970-01-01
                相关资源
                最近更新 更多