【问题标题】:How to set Toolbar background color in an activity to a color inside colors.xml file?如何将活动中的工具栏背景颜色设置为 colors.xml 文件中的颜色?
【发布时间】:2016-01-25 19:34:18
【问题描述】:

我的colors.xml 文件中有一种颜色,我需要将其用于toolbar 颜色

<resources>
    <color name="MAIN_A">#f16264</color>
</resources>

现在我需要使用MAIN_A 作为toolbar 的颜色。

【问题讨论】:

    标签: android android-layout android-activity android-studio android-actionbar


    【解决方案1】:

    使用此代码

    getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));
    

    【讨论】:

    • setBackgroundDrawable 已弃用。
    • @CoolMind 不正确。它在其他类中已弃用,但在 ActionBar.class 中不推荐
    • @GabrielBB,嗯,也许你是对的,但ActionBar 本身已被弃用,这里使用getSupportActionBar()
    【解决方案2】:

    这是 Kotlin 的代码

    supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))
    

    【讨论】:

      【解决方案3】:

      尝试新建布局资源toolbar.xml:

      <?xml version="1.0" encoding="utf-8"?>
      
      <android.support.v7.widget.Toolbar
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/MAIN_A" />
      

      然后将其包含在您的活动布局中,如下所示:

          <include
          android:id="@+id/toolbar"
          layout="@layout/toolbar" />
      

      那么你需要在你的activity的onCreate方法中设置这个工具栏:

          toolbar = (Toolbar) findViewById(R.id.toolbar);
          if (toolbar != null) {
              // set toolbar object as actionbar
              setSupportActionBar(toolbar);
          }
      

      之后,您可以通过 getSupportActionBar() 方法访问您的新操作栏。告诉我是否有帮助:)

      【讨论】:

        【解决方案4】:

        这样试试:

        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
        

        【讨论】:

          【解决方案5】:

          首先,ActionBar 已被弃用,请改用 Toolbar (android.widget.Toolbar)。 如果这不可行,请尝试以下 ActionBar 的支持:

          android.support.v7.app.ActionBar actionBar = getSupportActionBar();
          
          actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
          

          对于工具栏,它是:

          toolbar.setBackgroundResource(R.color.MAIN_A)
          

          【讨论】:

          • 如何更改工具栏的颜色??
          • @user5148540 toolbar.setBackgroundResource(R.color.MAIN_A)
          【解决方案6】:

          如果您有一个自定义 工具栏,考虑到 API 23+。

          1 - Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId);

          2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));

          【讨论】:

            【解决方案7】:

            工具栏背景文字箭头和三点弹出菜单颜色

            1) 背景:

            toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.toolbar_color));
            

            或(需要 API 16):

            toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.toolbar_color)));
            

            2) 标题:

            toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.gray);
            

            3) 箭头:

            toolbar.getNavigationIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray), PorterDuff.Mode.SRC_ATOP);
            

            4) 弹出菜单三点图标(右侧图标):

            toolbar.getOverflowIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray, PorterDuff.Mode.SRC_ATOP);
            

            请参阅 https://stackoverflow.com/a/26837072/2914140https://stackoverflow.com/a/51908890/2914140

            多合一(在 Kotlin 中):

            toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.blue))
            val toolbarTextColor = ContextCompat.getColor(this, R.color.gray)
            toolbar.setTitleTextColor(toolbarTextColor)
            toolbar.navigationIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
            toolbar.overflowIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
            

            【讨论】:

              【解决方案8】:

              根据您的问题,您尚未使用 SupportActionBar,因此您可以执行以下操作:

              ActionBar actionBar = getActionBar();
              actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
              actionBar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
              actionBar.setDisplayShowTitleEnabled(true);
              

              实际功劳归于 SO 的 this link

              【讨论】:

                【解决方案9】:

                这可能不是“最好的方法”,但我只是尝试过它并且效果很好,即使它应该被视为临时措施,直到有人找到更好的方法,并且它在 PRE-API23 之前有效...... 您可以为工具栏的每个单独使用创建一个新的 xml 布局,例如工具栏 1.xml、工具栏 2.xml 等。然后在活动代码中将其添加到 onCreate:

                对于工具栏1:

                getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorAdmin))); 
                
                LayoutInflater inflater_admin = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                
                @SuppressLint("InflateParams") View action_bar_view_admin = Objects.requireNonNull(inflater_admin).inflate(R.layout.chat_custom_bar_admin, null);
                
                actionBar.setCustomView(action_bar_view_admin);
                

                对于工具栏2:

                getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorOwner))); 
                
                LayoutInflater inflater_owner = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                
                @SuppressLint("InflateParams") View action_bar_view_owner = Objects.requireNonNull(inflater_owner).inflate(R.layout.chat_custom_bar_owner, null);
                
                actionBar.setCustomView(action_bar_view_owner);
                

                等等……

                希望这对某人有所帮助!

                【讨论】:

                  猜你喜欢
                  • 2017-06-02
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多