【问题标题】:Why can't I change background color of toolbar为什么我不能更改工具栏的背景颜色
【发布时间】:2017-12-03 08:57:21
【问题描述】:

我正在像这样更改工具栏颜色:

MainActivity.xml

    <Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></Toolbar>

styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">#0000ff</item>
    <item name="android:colorPrimaryDark">#ff0000</item>
    <item name="android:textColorPrimary">#fff</item>
    </style>

和 MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setActionBar(toolbar);

我可以改变颜色colorPrimaryDark和textColorPrimary但是不能改变colorPrimary,为什么?

我可以像这样改变工具栏的颜色:

toolbar.setBackgroundColor(Color.parseColor("#0000ff"));

但不能改变使用样式也为什么删除 setActionBar(toolbar);文本和标题溢出菜单消失了,为什么?

【问题讨论】:

  • 发布你的java代码,oncreate
  • 发布于创建

标签: java android android-actionbar toolbar


【解决方案1】:

来自 XML

android:background:"your color"

来自代码

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
ActionBar actionBar = getSupportActionBar();

actionBar.setBackgroundDrawable(Color.parse("your color"));

【讨论】:

    【解决方案2】:

    您可以从父主题名称中删除 NoActionBar

    【讨论】:

    • 那么我应该使用什么主题,因为如果我添加操作栏主题,它会在我自己的操作栏顶部显示为另一个操作栏
    【解决方案3】:

    把这些行

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     ActionBar actionBar = getSupportActionBar();
    
                actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
    

    【讨论】:

    • 我也可以通过toolbar.setbackgroundcolor改变你的方法和我做的有什么区别,还有为什么我们必须做getSupportActionbar或getActionbar,如果我删除这条线,三个点的actionbar (溢出菜单)消失了
    【解决方案4】:

    改用这个主题Theme.AppCompat.Light.NoActionBar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      相关资源
      最近更新 更多