繁体   English   中英

为什么不能更改工具栏的背景颜色

[英]Why can't I change background color of toolbar

我正在更改工具栏颜色,如下所示:

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);之后 文字和标题溢出菜单消失了,为什么?

从XML

android:background:"your color"

从代码

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

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

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

把这些线

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

            actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));

使用此主题Theme.AppCompat.Light.NoActionBar代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM