在android中设置android Listview的样式

时间:2014-02-15 09:01:45

标签: android android-listview

我试图在android

中设置Listview的样式

我想做什么 ::我想获得背景颜色和listview的文字颜色,如下图所示

[编辑] 我还想要选择行

的自定义点击效果

enter image description here

我目前拥有的东西 ::使用基础适配器开发的简单列表视图

我当前的listview快照 ::

enter image description here


main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     />
</LinearLayout>

ImageTextListBaseAdapterActivity.java

public class ImageTextListBaseAdapterActivity extends Activity {

    public static final String[] titles = new String[] { "Strawberry",
        "Banana", "Orange", "Mixed" };

    public static final String[] descriptions = new String[] {
        "It is an aggregate accessory fruit",
        "It is the largest herbaceous flowering plant", "Citrus Fruit",
    "Mixed Fruits" };

    public static final Integer[] images = { R.drawable.one,
        R.drawable.two, R.drawable.three, R.drawable.four };

    ListView listView;
    List<RowItem> rowItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.list);
        CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
        listView.setAdapter(adapter);
    }

}

Styles.xml

<resources>



    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

如何实现我的目标,获取我的listview,如第一个快照

所示

1 个答案:

答案 0 :(得分:1)

为了让背景像你提到的那样:

您的列表行xml应该是:

<RelativeLayout
android:id="@+id/relId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray">
    <TextView 
         android:id="@+id/txt"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textColor="@color/blue_color_here"/>
</RelativeLayout>

P.S:在颜色文件(color.xml资源文件)中定义blue_color_here

此外,在自定义适配器中传递此布局并使用此布局将项目填充到列表中。

对于单击效果,创建一个drawable并将listSelector的背景设置为drawable,并将相对布局的背景设置为drawable。

drawable可能看起来像:

<item android:state_pressed="true" android:drawable="@drawable/when_pressed"/>
<item android:drawable="@android:color/darker_gray"/>