简体   繁体   中英

Change view background color into a listview

I'm trying to change background color on a view component, but not success

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;
}

I can set some text in textview, but set color not working.

Can anybody helps me how to set the color? Thanks

The example color used is: -16711717

edit

Listview Item layout:

<?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>

background cannot be setted in xml, it's a dynamic color

You can directly set the background color of the component in the listview_accounts.xml file of your project. For example

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

You have to make a color.xml file in your values folder(maybe already present) and add the color value ex:-16711717. try this and let me know

 <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”));

you can find solution on This link

Just you have to change one thing. In delete.onclick method replace list.removeView(customView); with Customview.setbackground(Color.red); etc. This may help you. Best of luck.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM