Android 将视图背景颜色更改为listview

Android 将视图背景颜色更改为listview,android,android-listview,Android,Android Listview,我正在尝试更改视图组件的背景色,但没有成功 public View getView(int position, final View convertView, ViewGroup parent) { View view = convertView; try { if (view == null) { LayoutInflater vi = (LayoutInflater) mContext .getS

我正在尝试更改视图组件的背景色,但没有成功

public View getView(int position, final View convertView, ViewGroup parent) {
    View view = convertView;
    try {
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.listview_accounts, null); // --CloneChangeRequired(list_item)
        }
        final Account listItem = (Account) mList.get(position); // --CloneChangeRequired
        if (listItem != null) {

            int color = listItem.getColor();

            View vColor = (View) view
                    .findViewById(R.id.lv_account_view_color);

            vColor.setBackgroundColor(color);
            }
        }
    } catch (Exception e) {
    }
    return view;
}
我可以在textview中设置一些文本,但设置颜色不起作用

有人能帮我设置颜色吗?谢谢

使用的示例颜色为:-16711717

编辑

Listview项目布局:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View
    android:id="@+id/lv_account_view_color"
    android:layout_width="@dimen/activity_horizontal_margin"
    android:layout_height="wrap_content"
    android:background="#167117" />

<TextView
    android:id="@+id/lv_account_tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:textColor="@color/black" />

</LinearLayout>


不能在xml中设置背景,它是一种动态颜色

您可以直接在项目的listview_accounts.xml文件中设置组件的背景颜色。比如说

<component>
android:background="@color/color_name"
</component>

android:background=“@color/color\u name”
您必须在values文件夹(可能已经存在)中创建一个color.xml文件,并添加颜色值ex:-16711717。 试试这个,让我知道

 <View
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#167117" />

试试这个:vColor.setBackgroundColor(Color.parse(“#yourcolorcode”)

您可以在


你只需要改变一件事。在delete.onclick方法replace list.removeView(customView)中;带有Customview.setbackground(颜色:红色);等等。这可能对你有帮助。祝你好运。

我不能,我在颜色选择器中选择了那种颜色,它在我从列表视图出发时起作用,但在列表视图中,我不起作用……你试过用#like as#16711717定义这种颜色代码吗#16711717并在您的布局xml文件listview_帐户中使用它,正如我在回答中所定义的那样是的,我已经在listview中尝试过了,并且成功了!但是在listview中它不起作用!为什么我不能将backgorund颜色设置到listview项目中?您可以发布listview_accounts.xml的代码,在其中您尝试设置背景,以便我可以查看它。这个视图,还有其他类似这样的视图输出listview并工作,但是进入listview,不工作(此代码是listview项的一部分)