2012-08-13 22 views
16

私のアプリでは、アプリテーマ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> 

しかし、それは動作しません。何か案は?

答えて

0

おそらくこれもお手伝いできます。それは、ウェブサイトからです:あなたは(あなたのニーズに応じて、または活動)あなたのアプリケーションのための宣言カスタムテーマを持っている必要があります最初の

http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

初のもの。何かその後のような...

<!-- Somewhere in AndroidManifest.xml --> 
<application ... android:theme="@style/ThemeSelector"> 

、2例用のカスタムテーマを宣言し、ホロテーマとない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> 

+0

'values-v14'フォルダはどうですか? – Si8

+0

あなたはどういう意味ですか、それは何ですか? –

+0

すみません。私は、XMLの代わりにJavaを使ってどのように変更するのですか? – Si8

18
ActionBar bar = getActionBar(); 
bar.setBackgroundDrawable(new ColorDrawable("COLOR")); 

それは私がそれを使用して解決している私のためhere

+1

ありがとうございます。それは私のために働いた。 getSupportActionBar()。setBackgroundDrawable(new ColorDrawable(getResources()。getColor(R.color.material_blue_gray1))); – Ataru

+0

私の経験上、この設定には欠点があります。つまり、アクションバーのメニューの色は変わりません。 – Elye

8
<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> 

を働きました。

+3

私はこれを使用してsobedしています。 – zaptech

7
ActionBar actionBar; 

actionBar = getActionBar(); 
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#93E9FA"));  
actionBar.setBackgroundDrawable(colorDrawable); 
+2

'new ColorDrawable(0x93E9FA);' –

0

Androidのデフォルトアクションバーを使用している場合。あなたがjavaから変更すると、以前の色が表示されることがあります。

enter image description here

"app_bar_main" 内部次に、あなたのアクションバーコード。だから、app_bar_main.xmlの中に入り、単にBackgroundを追加してください。

<?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" /> 
あなたのレイアウトを持っている場合

0

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" /> 

はあなたの活動にこのコード

@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); 
} 
0

を配置する必要があり、単に解像度/値/スタイルに行きます。xmlファイルとxmlファイル.Hereの色を変更するには、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" /> 

の色を変更するためですそれがあなたを助けることを願って...

関連する問題