Android中ListView项目的边框

7
我希望能够将Edittext和Listview的边框设置如下图所示: enter image description here 我的XML代码如下:
    <LinearLayout
        android:background="#BDD6E0"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"  >

    </LinearLayout>

    <LinearLayout
        android:background="#BDD6E0"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"  >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Search"
            android:textColor="#000000"
            android:background="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/search_pat_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:singleLine="true"
            android:textColor="#000000"
            android:layout_weight="4">

        </EditText> 

    </LinearLayout>

    <LinearLayout
        android:background="#c0c0c0"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="6" >

            <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:layout_weight="1" >
            </ListView>

    </LinearLayout>

</LinearLayout>

我该如何在ListView的项中绘制边框?


我会创建一个简单的黑色矩形的九宫格,并将其设置为背景。 - Damien R.
可能是Android ListView项目边框宽度和颜色的重复问题。 - Sid M
6个回答

16

ListView的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">    
<stroke android:width="1dip" android:color="@color/edt_focused" />
</shape>

并向列表视图添加属性

android:divider="@drawable/list_divider" android:dividerHeight="1px"

android:color="@color/edt_focused" 的值将是什么? - osimer pothe
2
你想要的边框颜色 - Digvesh Patel
“listview的背景”是什么意思?您是说<ListView>应该有android:background=@drawable/myshape,其中myshape.xml是您的第一个编码块吗?而list_divider又是指什么? - LarsH

5

将列表子元素的背景设置为

 android:background="@drawable/shape"

然后在你的res>drawable文件夹中创建一个名为shape.xml的文件,内容如下:

示例代码:

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

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


    </shape>

3
创建一个xml drawable文件,例如/res/drawable/textlines.xml,并将其作为Edittext的背景属性赋值。
/res/drawable/textlines.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFDDDDDD" />

        </shape>
   </item>

   <item android:top="1dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
            <solid android:color="#00000000" />
        </shape>
   </item>

</layer-list>

You use this Drawable for EditText Background or List Item root Layout. 

3

要在ListView中设置边框,请设置属性android:divider="@drawable/img_list"android:dividerHeight="1px"以设置分隔线的高度。

使用以下代码设置EditText的边框。

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

所以你的EditText将会像这样。您可以根据需要更改颜色。 enter image description here

我遇到了问题。我也应用了listSelector,但选择器没有出现。 - user1410081

1
将以下形状设置为列表行项目的背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >




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

</shape>

0

我认为,如果你设置listview的transcriptMode属性,它也会显示边框。

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:transcriptMode="normal" >
</ListView>

所以不需要任何形状的 XML。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接