The background color of the selected item in listview (go)

Source: Internet
Author: User

By default, the background color of listview is black, and the highlighted color of the selected item is chrysanthemum yellow. In many cases, you have to define the background color or background image by yourself.

Android: cachecolorhint = "@ Android: color/transparent", Which means to remove the black background color. For example, the page will be refreshed when the listview is rolled. The default color is still the system color, so you can set it to transparent in this way, this attribute is useful when using rounded corner images in listview to set listview.

Android: divider = "@ null"Used to remove the black line between items in listview

1. Background Color

In list_item_color_bg.xml, you can set the color to achieve different colors when you click an item. However, if you use color, listview cannot use the Android: listselector attribute. If you set the Android: listselector method, after you click an item, the overall listview will all become a color. In this case, you must set Android: background in the item. Android: listselector applies to images, similar to (Android: drawable = "@ drawable/IMG ")

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@color/green"></item>    <item android:drawable="@color/white"></item></selector>

Color. xml

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="white">#ffffff</color>    <color name="black">#000000</color>    <color name="green">#00ff00</color></resources>

Next let's take a look at the layout file.

Listview. XML, in color format. listselector cannot be used here.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <ListView         android:id="@+id/lv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:fastScrollEnabled="true"        android:cacheColorHint="@android:color/transparent"        android:divider="@null"        /></LinearLayout>

List_item_color.xml: Set the background directly in the item layout through color settings.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:background="@drawable/list_item_color_bg">    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <TextView            android:id="@+id/tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20sp"        />        <TextView             android:id="@+id/info"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="14sp"            />    </LinearLayout></LinearLayout>

2. Background Image

This method uses an image in the selector file to set the item background. You can use either the Android: listselector method of listview or the Android: Background method of item, however, it is best to use the Android: Background method, because the default image set in the current selector file is used in the Android: listselector method.

(<Item Android: drawable = "@ drawable/login_input"/>) is not displayed, but can be changed to background. A little strange. I hope I can give some advice.

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/input_over"/>    <item android:drawable="@drawable/login_input"/></selector>

Listview is set as follows. Android: background is not set in item.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <ListView         android:id="@+id/lv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:fastScrollEnabled="true"        android:cacheColorHint="@android:color/transparent"        android:listSelector="@drawable/list_item_drawable_bg"        /></LinearLayout>

In this case, the background image is .9.png, and the corresponding white .9.png image is not displayed in login_input.

If you use Android: background to cancel Android: listselector, the effect is as follows:

The background color of the selected item in listview (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.