为什么设置 setBackgroundColor 在我的自定义 listView 中不起作用
我有一个自定义列表视图。主要布局 xml 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_height="wrap_content"
android:id="@+id/lv_clientes"
android:layout_width="0dp">
</ListView>
<!-- From this part there are not problems -->
</LinearLayout>
列表项 XML 是这样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlo_elemento"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<!-- From this part there are not problems -->
</RelativeLayout>
现在适配器是这样的:
public class AdapterListaClientes extends BaseAdapter
{
private Cliente[] data;
Context context;
LayoutInflater layoutInflater;
int itemSelected = -1;
public void setSelected(int valor)
{
itemSelected = valor;
}
public AdapterListaClientes(Context context, ArrayList<Cliente> data)
{
this.data = data.toArray(new Cliente[0]);
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
/*Mandatory things and so...*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//All the things that we should put in this point.. I'm using the list14 example
//HERE IS THE POLEMIC CODE
if(position == itemSelected)
convertView.setBackgroundColor(R.color.rojo);
else
convertView.setBackgroundColor(R.color.blanco);
return convertView;
} setBackgroundColor
() 方法不起作用。我知道这是在做一些事情,因为当我使用此方法时,按下列表视图项目时,按下的项目的背景颜色会更改为默认颜色的不透明版本。
这个问题仅发生在背景颜色上,我可以毫无问题地更改其他所有内容...
谢谢!
I have a custom listView. The main layout xml is something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_height="wrap_content"
android:id="@+id/lv_clientes"
android:layout_width="0dp">
</ListView>
<!-- From this part there are not problems -->
</LinearLayout>
The list item XML is this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlo_elemento"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<!-- From this part there are not problems -->
</RelativeLayout>
Now the adapter is like this:
public class AdapterListaClientes extends BaseAdapter
{
private Cliente[] data;
Context context;
LayoutInflater layoutInflater;
int itemSelected = -1;
public void setSelected(int valor)
{
itemSelected = valor;
}
public AdapterListaClientes(Context context, ArrayList<Cliente> data)
{
this.data = data.toArray(new Cliente[0]);
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
/*Mandatory things and so...*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//All the things that we should put in this point.. I'm using the list14 example
//HERE IS THE POLEMIC CODE
if(position == itemSelected)
convertView.setBackgroundColor(R.color.rojo);
else
convertView.setBackgroundColor(R.color.blanco);
return convertView;
}
}
The setBackgroundColor() method is not working. I know that is doing something because when I use this method the background color of the pressed item changes to a opaque version of the default color when a listview item is pressed.
This problem happen only with the background color, I can change everything else without problems...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它的声明是这样的。
Its declaration is like this.
当在视图上调用setBackgroundColor时,您需要将alpha值设置为非零值(例如0xFF),否则颜色将不会显示。
When calling setBackgroundColor on a view you need to set the alpha value to a non-zero value (e.g. 0xFF), otherwise the color will not show up.
要通过 setBackgroundColor 方法设置颜色,请执行以下操作:-
可以通过
To set color by setBackgroundColor method do this:-
either by
您也可以使用 setBackgroundColor() 但您需要了解它需要一个对象而不是资源 id。因此,您必须将资源转换为颜色对象,如下所示:
You could also use setBackgroundColor() but you'll need to understand that is expects an object not a resource id. So you'd have to convert the resource to a color object, like so:
使用
R.color.rojo 是一种资源,它不是颜色。
Use
R.color.rojo is a resource, it is not color..