Android: Theme ContextMenu item selection

2019-05-27 10:54发布

问题:

How can I theme the list seletor in a ContextMenu? For ListViews I used the way described here:

http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

So I assigned the created style to my theme like this:

<style name="Theme...." parent="@android:style/Theme.Light">
    <item name="android:listViewStyle">@style/ListView</item>
</style>

回答1:

Try Override context menu colors in Android and see if it helps. It says you can't override the standard context menu selector but goes on to give code for context on long-press.

To theme any list item I assign a background on the lists xml, such as android:background="@layout/customshape, custon shape being something like:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient android:startColor="@color/white" 
      android:endColor="@color/light_grey_background" 
      android:angle="270"/> 
<corners 
android:bottomRightRadius="7dp" 
android:bottomLeftRadius="7dp" 
android:topLeftRadius="7dp" 
android:topRightRadius="7dp"/> 
</shape>

Which I call from the list item .xml with something like:

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:paddingTop="2dip"
 android:paddingBottom="2dip"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@layout/customrecipeshape"
     android:orientation="horizontal" >

 <ImageView 
     android:id="@+id/listicon" 
     android:layout_width="34px" 
     android:layout_height="wrap_content" 
     android:src="@drawable/icon_red" 
 />
 <TextView android:id="@+id/thing1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="@color/black"
     android:textAppearance="?android:attr/textAppearanceSmall"/>    


 <TextView android:id="@+id/thing2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20px" 
        android:textColor="@color/grey21"
        android:textAppearance="?android:attr/textAppearanceSmall"/>
 <TextView 
     android:id="@+id/thing3"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="@color/grey21"
     android:textAppearance="?android:attr/textAppearanceSmall"
     />




  </LinearLayout>      

And as stated here: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/GY9DFX2H-Bc

You can't override the custom context menu, but you can make your own using a listview and an alert dialog.