I'd seperti untuk mengubah warna latar belakang pilihan (overflow) menu di Android 4.2. Saya telah mencoba semua metode tapi itu masih menunjukkan warna default yang ditetapkan oleh tema. Saya menggunakan kode berikut & XML konfigurasi.
MainActivity.java
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setTitle("Sample Menu");
getActionBar().setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#33B5E5")));
int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView titleText = (TextView)findViewById(titleId);
titleText.setTextColor(Color.parseColor("#ffffff"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
setMenuBackground();
return true;
}
protected void setMenuBackground(){
// Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory( new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView( name, null, attrs );
/* The background gets refreshed each time a new item is added the options menu.
* So each time Android applies the default background we need to set our own
* background. This is done using a thread giving the background change as runnable
* object */
new Handler().post( new Runnable() {
public void run () {
// sets the background color
view.setBackgroundResource( R.color.menubg);
// sets the text color
((TextView) view).setTextColor(Color.WHITE);
// sets the text size
((TextView) view).setTextSize(18);
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}});
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:icon="@drawable/menu"
android:showAsAction="always"
android:title="@string/action_settings">
<menu>
<item
android:id="@+id/item1"
android:showAsAction="always"
android:title="@string/item1" />
<item
android:id="@+id/item2"
android:showAsAction="always"
android:title="@string/item2" />
<item
android:id="@+id/item3"
android:showAsAction="always"
android:title="@string/item3" />
<item
android:id="@+id/item4"
android:showAsAction="always"
android:title="@string/item4" />
</menu>
</item>
</menu>
color.xml
<color name="menubg">#33B5E5</color>
Di atas setMenuBackground tidak mengambil efek:
Pada gambar di atas, saya ingin mengubah menu latar belakang dari warna hitam ke warna biru di Bar Tindakan. Bagaimana saya bisa mencapai ini, dan apa yang saya lakukan salah?
The Aksi Bar Style Generator, disarankan oleh Sunny, adalah sangat berguna, tetapi menghasilkan banyak dari file, yang sebagian besar tidak relevan jika anda hanya ingin mengubah warna latar belakang.
Jadi, saya menggali lebih dalam ke dalam kantong itu menghasilkan, dan mencoba untuk mempersempit apa saja bagian-bagian yang penting, sehingga saya dapat membuat jumlah minimum dari perubahan untuk aplikasi saya. Di bawah ini adalah apa yang saya temukan.
Dalam gaya generator, setelan yang relevan adalah Popup warna, yang mempengaruhi "Overflow menu, submenu dan spinner panel latar belakang".
Pergi dan menghasilkan zip, tapi dari semua file yang dihasilkan, anda hanya benar-benar membutuhkan satu gambar, menu_dropdown_panel_example.9.png
, yang terlihat seperti ini:
Jadi, tambahkan resolusi yang berbeda versi dari itu untuk res/drawable-*
. (Dan mungkin mengubah nama mereka untuk menu_dropdown_panel.9.png
.)
Maka, sebagai contoh, dalam res/values/themes.xml
anda akan memiliki yang berikut, dengan android:popupMenuStyle
dan android:popupBackground
menjadi kunci pengaturan.
<resources>
<style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
<item name="android:actionBarStyle">@style/MyApp.ActionBar</item>
</style>
<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
</style>
<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<!-- Blue background color & black bottom border -->
<item name="android:background">@drawable/blue_action_bar_background</item>
</style>
</resources>
Dan, tentu saja, dalam AndroidManifest.xml
:
<application
android:theme="@style/MyAppActionBarTheme"
... >
Apa yang anda dapatkan dengan setup ini:
Perhatikan bahwa I'm menggunakan Tema.Holo.Cahaya
sebagai dasar tema. Jika anda menggunakan Tema.Holo
(Holo Gelap), ada's langkah tambahan yang diperlukan sebagai jawaban ini menggambarkan!
Juga, jika anda (seperti saya) ingin gaya seluruh Tindakan Bar, bukan hanya menu, menempatkan sesuatu seperti ini dalam res/drawable/blue_action_bar_background.xml
:
<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF000000" />
<solid android:color="#FF2070B0" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF2070B0" />
<solid android:color="#00000000" />
<padding android:bottom="2dp" />
</shape>
</item>
</layer-list>
Karya besar setidaknya pada Android 4.0 (API 14 tingkat+).
Dalam kasus orang-orang yang masih berada di untuk solusi yang bekerja, di sini adalah apa yang bekerja untuk saya: - Ini adalah untuk Appcompat dukungan perpustakaan. Ini adalah kelanjutan untuk styling ActionBar dijelaskan di sini
Berikut adalah styles.xml file.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- This is the styling for action bar -->
<item name="actionBarStyle">@style/MyActionBar</item>
<!--To change the text styling of options menu items</item>-->
<item name="android:itemTextAppearance">@style/MyActionBar.MenuTextStyle</item>
<!--To change the background of options menu-->
<item name="android:itemBackground">@color/skyBlue</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@color/red</item>
<item name="titleTextStyle">@style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/red</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">25sp</item>
</style>
</resources>
dan ini adalah bagaimana hal itu tampak--MenuItem latar belakang warna biru langit dan MenuItem teks warna merah muda dengan textsize sebagai 25sp:--
Ada cara mudah untuk mengubah warna Actionbar
Gunakan ActionBar Generator dan copy paste semua file di res
folder dan mengubah tema di Android.file manifest.
Jika anda membaca ini, it's mungkin karena semua jawaban sebelumnya tidak't bekerja untuk anda Holo Gelap berdasarkan tema.
Holo Gelap menggunakan tambahan pembungkus untuk PopupMenus, jadi setelah melakukan apa yang Jonik menyarankan anda harus menambahkan kode berikut gaya 'xml' file:
<style name="PopupWrapper" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/YourPopupMenu</item>
</style>
Kemudian referensi anda dalam tema blok:
<style name="Your.cool.Theme" parent="@android:style/Theme.Holo">
.
.
.
<item name="android:actionBarWidgetTheme">@style/PopupWrapper</item>
</style>
Yang's itu!
Saya trik sederhana untuk mengubah warna latar belakang dan warna teks pada Popup Menu / Menu Pilihan
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/MyPopupMenu</item>
<item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>
<!-- Popup Menu Background Color styles -->
<style name="MyPopupMenu"
parent="@android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">@color/Your_color_for_background</item>
</style>
<!-- Popup Menu Text Color styles -->
<style name="TextAppearance">
<item name="android:textColor">@color/Your_color_for_text</item>
</style>
Dalam kasus anda bekerja di terbaru toolbar di android studio maka di sini adalah yang terkecil solusi Untuk mengubah toolbar menu pilihan warna, tambahkan ke toolbar elemen
app:popupTheme="@style/MyDarkToolbarStyle"
Kemudian pada anda styles.xml menentukan popup menu style
`<nama style="MyDarkToolbarStyle" orang tua="ThemeOverlay.AppCompat.Cahaya">
<nama barang="android:colorBackground">@color/mtrl_white_100 <nama barang="android:textColor">@color/mtrl_light_blue_900 `Perhatikan bahwa anda perlu menggunakan colorBackground bukan latar belakang. Yang terakhir akan diterapkan untuk segala sesuatu (menu itu sendiri dan masing-masing item menu), mantan hanya berlaku untuk menu popup.
I'm juga dipukul dengan masalah yang sama, akhirnya saya punya solusi sederhana. hanya menambahkan satu baris untuk tindakan bar gaya.
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">@color/colorAccent</item>
<item name="android:colorBackground">@color/colorAppWhite</item>
</style>
"android:colorBackground" cukup untuk mengubah pilihan menu latar belakang
Cara CEPAT!
styles.xml
<style name="popupTheme" parent="Theme.AppCompat.Light">
<item name="android:background">@color/colorBackground</item>
<item name="android:textColor">@color/colorItem</item>
</style>
Kemudian tambahkan ini gaya tertentu untuk anda AppTheme gaya
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="popupTheme">@style/popupTheme</item>
</style>
SELESAI!
Anda dapat menerapkan gaya dan Tema yang di Meluap MenuItem sesuai di bawah ini. Menu OverFlow adalah ListView jadi, kita dapat menerapkan tema sesuai listview.
Terapkan kode di bawah ini di styles.xml
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
<item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<item name="android:listPreferredItemHeightSmall">40dp</item>
</style>
<!-- Change Overflow Menu ListView Divider Property -->
<style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
<item name="android:divider">@color/app_navigation_divider</item>
<item name="android:dividerHeight">1sp</item>
<item name="android:listSelector">@drawable/list_selector</item>
</style>
<!-- Change Overflow Menu ListView Text Size and Text Size -->
<style name="PopupMenuTextView" parent="@android:style/Widget.Holo.Light.TextView">
<item name="android:textColor">@color/app_white</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">18sp</item>
<item name="android:drawablePadding">25dp</item>
<item name="android:drawableRight">@drawable/navigation_arrow_selector</item>
</style>
<!-- Change Overflow Menu Background -->
<style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_overflow_bg</item>
</style>
Saya bisa mengubah warna dari tindakan yang melimpah hanya dengan memasukkan nilai hex pada:
<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">HEX VALUE OF COLOR</item>
</style>
Dalam aplikasi tema, anda dapat mengatur android:itemBackground properti untuk mengubah warna dari menu tindakan.
Misalnya:
<style name="AppThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/drk_colorPrimary</item>
<item name="colorPrimaryDark">@color/drk_colorPrimaryDark</item>
<item name="colorAccent">@color/drk_colorAccent</item>
<item name="actionBarStyle">@style/NoTitle</item>
<item name="windowNoTitle">true</item>
<item name="android:textColor">@color/white</item>
<!-- THIS IS WHERE YOU CHANGE THE COLOR -->
<item name="android:itemBackground">@color/drk_colorPrimary</item>
</style>
Tambahkan berikut ini untuk anda Styles.xml
<style name="Custom_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">@color/white</item>
<item name="android:textColor">@color/colorPrimaryDark</item>
</style>
Mengubah tema di activity_main.xml
hanya.
android:theme="@style/Custom_Theme"
Coba kode ini. Tambahkan potongan ini untuk anda res>nilai>styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarWidgetTheme">@style/Theme.stylingactionbar.widget</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">@color/DarkSlateBlue</item>
<!-- for @color you have to create a color.xml in res > values -->
</style>
<style name="Theme.stylingactionbar.widget" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
Dan di Manifest.xml tambahkan berikut cuplikan di bawah aplikasi
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Saya menggunakan ini dan itu bekerja dengan baik.
Menerapkan kode ini di onCreate() fungsi
val actionBar: android.support.v7.app.ActionBar? = supportActionBar
actionBar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("Your color code here")))
<style name="customTheme" parent="any_parent_theme">
<item name="android:itemBackground">#424242</item>
<item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#E9E2BF</item>
</style>
Berikut ini adalah perubahan-perubahan yang diperlukan dalam tema anda untuk mengubah tindakan bar & overflow menu warna latar belakang. Anda perlu mengkonfigurasi "android:latar belakang" dari actionBarStyle & popupMenuStyle
<application
android:name="...."
android:theme="@style/AppLightTheme">
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBarLight</item>
<item name="android:popupMenuStyle">@style/ListPopupWindowLight</item>
</style>
<style name="ActionBarLight" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/white</item>
</style>
<style name="ListPopupWindowLight"parent="@android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:background">@color/white</item>
</style>