I'm trying to create a custom listview row color inside my adapter
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:color="#6495ED" />
<item android:state_pressed="true"
android:color="#ffffff" />
<item android:state_selected="true"
android:state_pressed="false"
android:color="#E5F5FA" />
</selector>
and here is my code inside adapter
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.CategoryPreview, null, false);
}
TextView txtCategoryName = row.FindViewById<TextView>(Resource.Id.txtCategoryName);
txtCategoryName.Text = mitems[position].CategoryName;
TextView txtCategoryID = row.FindViewById<TextView>(Resource.Id.txtCategoryID);
txtCategoryID.Text = mitems[position].CategoryID;
row.SetBackgroundResource(Resource.Drawable.artists_list_backgroundcolor);
return row;
}
When activity is going to start i'm getting error
Android.Content.Res.Resources+NotFoundException: Drawable WiOrderAndroid.WiOrderAndroid:drawable/artists_list_backgroundcolor with resource ID #0x7f020053
It work only if i will set my xml with this way.
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#ef4444" />
</shape>
</item>
</selector>
But is this the right way?
尝试改用R.drawable.artists_list_backgroundcolor
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:color="#6495ED" />
<item android:state_pressed="true"
android:color="#ffffff" />
<item android:state_selected="true"
android:state_pressed="false"
android:color="#E5F5FA" />
</selector>
From above codes, what you have defined is a ColorStateList , it should be in the res/color/artists_list_backgroundcolor.xml
path, not res/drawable/
, and it should be referred by @color/artists_list_backgroundcolor
or Resource.Color.artists_list_backgroundcolor
.
What you need is a drawable selector
. Please refer to this
SetBackgroundResource
method:
Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
The resource should be in drawable
folder, but your artists_list_backgroundcolor
file belongs to ColorStateList , it should be in the /res/color/
folder, not drawable
folder.
If you want to use your artists_list_backgroundcolor
file ,you need put it in the /res/color/
folder. But you can't use it by row.SetBackgroundResource(Resource.Drawable.artists_list_backgroundcolor);
, please refer to this to use the ColorStateList
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.