【问题标题】:Why set setBackgroundColor is not working in my custom listView为什么设置 setBackgroundColor 在我的自定义 listView 中不起作用
【发布时间】:2011-11-09 19:08:24
【问题描述】:

我有一个自定义列表视图。主要布局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() 方法不起作用。我知道这是在做某事,因为当我使用此方法时,按下列表视图项目时,按下项目的背景颜色会变为默认颜色的不透明版本。

这个问题只发生在背景颜色上,我可以毫无问题地更改其他所有内容...

谢谢!

【问题讨论】:

    标签: android listview adapter


    【解决方案1】:

    使用

    setBackgroundResource(R.color.rojo);
    

    R.color.rojo 是一种资源,它不是颜色..

    【讨论】:

    • 哇!它有效!...为什么他们使用 setBackgroundResource() 而不是 setBackgroundColor() ...?这几乎违反常识......
    • 我必须使用.setImageResource(R.color.color_placeholder); 否则它不会更新之前的图像。
    • 兄弟!你是救生员。我不知道为什么 setBackground 和 setBackgroundColor 不起作用
    【解决方案2】:

    您也可以使用 setBackgroundColor() 但您需要了解它需要一个对象而不是资源 ID。因此,您必须将资源转换为颜色对象,如下所示:

    setBackgroundColor(getResources().getColor(R.color.rojo));
    

    【讨论】:

      【解决方案3】:

      要通过 setBackgroundColor 方法设置颜色:-

      setBackgroundColor(Color.parseColor("#e7eecc"));
      

      通过

      setBackgroundResource(R.color.<Your-Color>)
      

      【讨论】:

        【解决方案4】:
        .setBackgroundColor(getResources().getColor(R.color.raj));
        

        【讨论】:

          【解决方案5】:
          @RemotableViewMethod
          public void setBackgroundColor(@ColorInt int color) {
              if (mBackground instanceof ColorDrawable) {
              ((ColorDrawable) mBackground.mutate()).setColor(color);
              computeOpaqueFlags();
              mBackgroundResource = 0;
              }else {
              setBackground(new ColorDrawable(color));
              }
          }
          

          它的声明是这样的。

          【讨论】:

            猜你喜欢
            • 2012-07-09
            • 2018-05-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-12-04
            • 1970-01-01
            • 1970-01-01
            • 2023-04-03
            相关资源
            最近更新 更多