Change ListView Item background color but keep the default selector style?

后端 未结 4 544
暗喜
暗喜 2020-12-18 01:24

I am trying to programmatically set a custom background color in my ListView Adapter but I want to keep the default Listview Selector style from Android.

I am settin

相关标签:
4条回答
  • 2020-12-18 02:07

    Did you try to set the property android:drawSelectorOnTop of your ListView to true ?
    You will be able to set a background for your item and to see the item highlighted when pressed.

    0 讨论(0)
  • 2020-12-18 02:14

    Try write your custom adapter and set rowView BackgroundColor like that(if position odd number BLACK, even RED). also you can send a color list to adapter and set different color for every rowView

    public View getView(int position, View convertView, ViewGroup parent) {
                View rowView = convertView;
            enter code here
    
                if(rowView == null)
                {
                    if((position % 2)==1){ 
                    rowView.setBackgroundColor(Color.BLACK)
                        //colorList :setBackgroundColor( colorList.get(position) )
                    }
                     else{
                        rowView.setBackgroundColor(Color.RED)
                    }
                 }           
                return rowView;
            }
    
    0 讨论(0)
  • 2020-12-18 02:15

    Try to apply this style to your ListView:

    <style name="List">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:background">#FFFFFF</item>
        <item name="android:dividerHeight">1dp</item>
        <item name="android:divider">#CCCCCC</item>
        <item name="android:cacheColorHint">#FFFFFF</item>
        <item name="android:listSelector">#00000000</item>
    </style>
    

    And in your xml layout this:

    <ListView 
        android:id="@+id/listView"
        style="@style/List"
        android:paddingTop="1dp"/>
    
    0 讨论(0)
  • 2020-12-18 02:22

    Try this one :

    in your Row layout set the background properties for

    android:background="@drawable/your_selector"
    

    and your listView set

    android:listSelector="@drawable/your_selector"
    
    0 讨论(0)
提交回复
热议问题