本文介绍了将样式应用到 Android ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to style the lisview in my application as seen is this image

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

I have tried to develop it by applying gradient :

list_item_normal 的代码是:

code for list_item_normal is :

<?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 的代码是:

code for list_item_pressed.xml is :

<?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 的代码:

code for 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

and 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 上应用了这种样式,例如:

and i have applied this style on custom listitem like :

<TextView
    .../>

<ImageView
   ... />

我有 lisview 的 main.xml 是:

and the main.xml in which i have lisview is :

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

我得到如下所示的输出:

and i am getting output as shown below :

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

Please help me by telling what i am doing wrong..

谢谢施鲁提

推荐答案

您了解抖动吗?检查这个:http://android.amberfog.com/?p=247.

Do you know about Dithering? Check this: http://android.amberfog.com/?p=247.

还要检查这些问题:

  1. 如何绘制平滑Android 中画布上的/dithered 渐变
  2. Android 视图抖动

是的,我建议为您的 LitsView 创建自定义适配器,您只需创建一个自定义行 xml 布局文件并应用一次背景图像.

And yes, i would suggest to create custom adapter for your LitsView, in which you just have to create one custom row xml layout file and apply the background image for one time.

这篇关于将样式应用到 Android ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:11