将样式应用于Android ListView

时间:2011-12-19 07:11:14

标签: android styles android-listview

我想在我的应用程序中设置lisview样式,如图所示

Expected Output

我试图通过应用渐变来开发它:

list_item_normal的代码是:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:centerColor="#E6E6E6"
        android:endColor="#CCCCCC"
        android:startColor="#FFFFFF" 
        android:angle="270"/>
    <!--
        <gradient
        android:startColor="#FF7500"
        android:centerColor="#FFCC00"
        android:endColor="#FF7500"
        android:angle="270"/>
    -->

    <stroke
        android:width="1dp"
        android:color="#A0000000" />

    <padding
        android:bottom="8dp"
        android:left="5dp"
        android:right="5dp"
        android:top="8dp" />

    <corners android:radius="5dp" />

</shape>
list_item_pressed.xml的

代码是:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">

    <gradient
        android:centerColor="#E6E6E6"
        android:endColor="#CCCCCC"
        android:startColor="#FFFFFF" android:angle="270"/>
    <!--
         <gradient android:startColor="#FF66CFE6" android:centerColor="#FF207FB9" 
        android:endColor="#FF0060B8" android:angle="270"/>
    -->

    <stroke
        android:width="2dp"
        android:color="#80000000" />

    <padding
        android:bottom="8dp"
        android:left="5dp"
        android:right="5dp"
        android:top="8dp" />

    <corners android:radius="7dp" />

</shape>

list_item_pressed.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">

    <gradient
        android:centerColor="#E6E6E6"
        android:endColor="#CCCCCC"
        android:startColor="#FFFFFF" android:angle="270"/>
    <!--
         <gradient android:startColor="#FF66CFE6" android:centerColor="#FF207FB9" 
        android:endColor="#FF0060B8" android:angle="270"/>
    -->

    <stroke
        android:width="2dp"
        android:color="#80000000" />

    <padding
        android:bottom="8dp"
        android:left="5dp"
        android:right="5dp"
        android:top="8dp" />

    <corners android:radius="7dp" />

</shape>

和list_gradient.xml

<?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/list_item_pressed" />

    <item android:state_focused="true"
          android:drawable="@drawable/list_item_selected" />

    <item android:drawable="@drawable/list_item_normal" />

</selector>

我在custom listitem上应用了这种风格,如:

<TextView
    .../>

<ImageView
   ... />

和我有lisview的main.xml是:

<ListView
    android:id="@+id/List"
    style="@style/list_item_gradient"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:clickable="true" >
</ListView>

我正在获得如下所示的输出:

current output

请告诉我我做错了什么来帮助我。

由于 思鲁提

2 个答案:

答案 0 :(得分:5)

你知道Dithering吗?检查一下:http://android.amberfog.com/?p=247

同时检查以下问题:

  1. How to draw a smooth/dithered gradient on a canvas in Android
  2. Android view dithering
  3. 是的,我建议为LitsView创建自定义适配器,您只需创建一个自定义行xml布局文件并应用背景图像一次。

答案 1 :(得分:1)

创建customAdapter扩展baseAdapter。创建list_item.xml并将其所需的外观设置选择器赋予其背景。 就是这样。