如何更改操作栏切换颜色? - android

时间:2017-01-15 06:25:30

标签: android android-layout android-actionbar android-xml android-styles

如何更改操作栏切换的颜色?

enter image description here

以下是我在java中操作栏的代码:

  //Navigation view and Drawer layout
        drawerlayout = (DrawerLayout) findViewById(R.id.main_ND);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerlayout,0,0);
        navigationView = (NavigationView) findViewById(R.id.main_NV);
        navigationView.setItemIconTintList(null);
        View headerView = navigationView.getHeaderView(0);
        nD_image = (ImageView) headerView.findViewById(R.id.nd_image);
        Glide.with(getBaseContext()).load(R.drawable.my_paper).centerCrop().into(nD_image);
        setNavigationView();
        drawerlayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
}


 @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(actionBarDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

以我的风格

  <style name="my_theme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimaryDark">@color/salmonDark</item>
        <item name="colorPrimary">@color/salmon_main</item>
        <item name="colorAccent">@color/black</item>
        <item name="android:actionMenuTextColor">@color/white</item><!-- green was: #8BC34A -->
        <item name="android:actionMenuTextAppearance">@style/actionBarTextSize</item>
    </style>

然后在我的清单中

 <activity android:name=".Main.Main"
            android:screenOrientation="portrait"
            android:theme="@style/zivit_theme"/>

我完全不确定如何更改颜色或在何处更改颜色。我希望有人可以在一步一步的过程中发布一些示例代码(或者那个神奇的代码行代码)谢谢!

2 个答案:

答案 0 :(得分:4)

试试这个:

基本主题中的

:为操作栏添加项目

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

自定义操作栏主题:

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
    <item name="android:background">@color/background</item>

     <!-- Support library compatibility -->
     <item name="titleTextStyle">@style/TitleTextStyle</item>
     <item name="background">@color/background</item>
</style>

您还可以修改操作栏文字颜色:

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/foreground</item>
</style>

切换按钮样式:

 <style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/foreground</item>
</style>

答案 1 :(得分:0)

在API级别11之前,您可以定义操作栏的颜色,如下所示

<resources> 
     <style name="MyTheme" parent="@android:style/Theme.Holo.Light"> 
        <item name="android:actionBarStyle">@style/MyActionBar</item> </style> 
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
   <item name="android:background">ANY_HEX_COLOR_CODE</item> </style> 
  </resources>