[英]Change action bar color in android
我在我的應用程序中使用應用程序主題Theme.Black
。 在這個主題中,動作欄是灰色的。 我如何改變動作欄的顏色? 這是我的嘗試:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="@android:style/Theme.Black" >
</style>
<style name="Widget.MyApp.ActionBar" parent="@android:style/Widget.ActionBar">
<item name="android:background">@android:color/black</item>
</style>
</resources>
但是,它不起作用。 有任何想法嗎?
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#C1000E</item>
<item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="AppTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.StatusBar.Title">
<item name="android:textColor">#E5ED0E</item>
</style>
我已經解決了使用它。
ActionBar actionBar;
actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#93E9FA"));
actionBar.setBackgroundDrawable(colorDrawable);
只需轉到res / values / styles.xml文件並編輯xml文件即可更改xml文件的顏色。這是示例代碼
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
//下面的代碼用於更改操作欄的顏色
<item name="colorPrimary">"type your color code here. eg:#ffffff"</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
希望它能幫到你......
也許這也可以幫到你。 它來自網站:
http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/
首先,您需要為您的應用程序(或活動,根據您的需要)聲明一個自定義主題。 就像是…
<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">
然后,為兩種情況聲明您的自定義主題,包含和不包含Holo主題的API版本。 對於舊主題,我們將自定義windowTitleBackgroundStyle屬性,對於較新的主題,我們將自定義ActionBarStyle。
<!-- res/values/styles.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ThemeSelector" parent="android:Theme.Light">
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
<style name="WindowTitleBackground">
<item name="android:background">@color/title_background</item>
</style>
</resources>
<!-- res/values-v11/styles.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ThemeSelector" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBar</item>
</style>
<style name="ActionBar" parent="android:style/Widget.Holo.ActionBar">
<item name="android:background">@color/title_background</item>
</style>
</resources>
而已!
如果您使用Android默認操作欄。 如果從java更改,則有時會顯示以前的顏色。
例
然后你的行動條碼在“app_bar_main”里面。 所以進入app_bar_main.xml並添加背景。
例
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#33691e" <!--use your color -->
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main1" />
如果你的布局中有activity_main
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
你必須把
在你的Activity中這段代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Color.CYAN);
}
更新的代碼:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.your_color)));
onclick按鈕c2
根據按鈕的背景獲取並更改操作欄和通知欄的顏色,希望這有幫助
int color = c2.getBackground()).getColor();
int colorlighter = -color * 40 / 100 + color;
getWindow().setNavigationBarColor(colorlighter);
getWindow().setStatusBarColor(colorlighter);
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(color));
colorlighter
用於設置通知欄的顏色比操作欄輕一點
color
是按鈕c2
的背景顏色,根據該顏色我們改變顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.