Android 4.2'de seçenek (taşma) menüsünün arka plan rengini değiştirmek istiyorum. Tüm yöntemleri denedim ancak hala tema tarafından ayarlanan varsayılan rengi gösteriyor. Aşağıdaki kodu & XML yapılandırmalarını kullandım.
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;
}});
}
}
Menü.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>
Yukarıdaki setMenuBackground herhangi bir etki yaratmıyor:
Yukarıdaki resimde, Eylem Çubuğundaki menü arka planını siyahtan mavi renge değiştirmek istiyorum. Bunu nasıl başarabilirim ve neyi yanlış yaptım?
Sunny tarafından önerilen][2] Action Bar Style Generator çok kullanışlıdır, ancak yalnızca arka plan rengini değiştirmek istiyorsanız çoğu alakasız olan çok sayıda dosya üretir.
Bu yüzden, oluşturduğu zip'i daha derinlemesine inceledim ve uygulamamda minimum miktarda değişiklik yapabilmek için önemli olan kısımları daraltmaya çalıştım. Aşağıda bulduklarım yer alıyor.
Stil oluşturucuda ilgili ayar, "Akış menüsü, alt menü ve döndürücü panel arka planını" etkileyen Açılır pencere rengidir.
Devam edin ve zip'i oluşturun, ancak oluşturulan tüm dosyalardan yalnızca bir resme ihtiyacınız var, menu_dropdown_panel_example.9.png
, bu da şuna benzer bir şeye benziyor:
Bu yüzden, farklı çözünürlükteki versiyonlarını res/drawable-*
içine ekleyin. (Ve belki de onları menu_dropdown_panel.9.png
olarak yeniden adlandırın).
Ardından, örnek olarak, res/values/themes.xml
içinde android:popupMenuStyle
ve android:popupBackground
anahtar ayarlar olmak üzere aşağıdakilere sahip olursunuz.
<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>
Ve tabii ki AndroidManifest.xml
içinde:
<application
android:theme="@style/MyAppActionBarTheme"
... >
Bu kurulumla ne elde edersiniz:
Temel tema olarak Theme.Holo.Light
kullandığımı unutmayın. Eğer Theme.Holo
(Holo Dark) kullanıyorsanız, bu cevapta açıklandığı gibi ek bir adım daha gereklidir!
Ayrıca, (benim gibi) sadece menüyü değil, tüm Eylem Çubuğunu şekillendirmek istiyorsanız, res/drawable/blue_action_bar_background.xml
dosyasına buna benzer bir şey koyun:
<!-- 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>
En azından Android 4.0+ (API seviyesi 14+) üzerinde harika çalışıyor.
Actionbar'da renkleri değiştirmenin kolay bir yolu vardır
ActionBar Generator]1 kullanın ve tüm dosyayı res
klasörünüze kopyalayıp yapıştırın ve Android.manifest dosyasında temanızı değiştirin.
Sadece hex değeri koyarak eylem taşmasının rengini değiştirebildim:
<!-- 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>