如何在每个ListView项目中圆角...?

时间:2014-03-02 13:45:21

标签: android listview

所以,我遇到了一个问题。我有一个看起来像这样的ListView:

enter image description here

好吧,我一直在寻找解决问题的答案。所以我的问题是,我不能在每个项目中圆角,我能够绕过所有列表视图。这是我的listview样式代码:

<?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="vertical"
    android:background="@color/green">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/red"
        android:dividerHeight="8dp"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/listviewstyle">
    </ListView>

</LinearLayout>

有人可以帮我找出如何围绕每个listview项目角落吗?

4 个答案:

答案 0 :(得分:2)

您应该将style应用于列表的每个元素,而不是将其应用于整个列表。

答案 1 :(得分:1)

设置

android:background="@drawable/listviewstyle"

到列表中的每个项目

答案 2 :(得分:1)

创建shape-drawable并为radius提供您需要的任何值

custom_list_item.xml (在drawable文件夹中)。我将这些属性用于自定义Toast,但您可以根据需要更改

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
 <solid android:color="@drawable/white"/>
    <corners
      android:bottomRightRadius="45dp"
      android:bottomLeftRadius="45dp"
      android:topLeftRadius="45dp"
      android:topRightRadius="45dp"/>
    <stroke android:color="@drawable/orange"
        android:width="3px"/>
</shape>

然后为layout项目创建ListView。您可以将此shape-drawable设为列表项的背景,或将其放在styles.xml中的样式中,并将样式应用于ListViewlayout

styles.xml 中创建样式

<style name="CustomListItem" parent="android:Widget.TextView">
    <item name="android:background">@drawable/custom_list_item</item>
    <item name="android:textColor">@drawable/black</item>
</style>

然后在自定义列表项layout中添加

style="@style/CustomListItem"

答案 3 :(得分:1)

您目前正在将您的样式应用于整个列表,您确实需要在适配器上设置它,而不是在整个列表中设置。

在您的示例中,您需要从ListView XML中删除此行:

android:background="@drawable/listviewstyle"

然后在适配器中使用list元素,将其放在那里。 (提示:这是用于描述“元素1”和“子元素1”的布局,此处未包含该元素。)