android中listview项的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21253623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:13:36  来源:igfitidea点击:

border of items of listview in android

androidxmllistview

提问by osimer pothe

I want to set border of Edittext and items of listview as in the picture :

我想设置 Edittext 的边框和列表视图的项目,如图所示:

enter image description here

在此处输入图片说明

My xml code is as following :

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

What can I do to draw border in the items of listview ?

我该怎么做才能在 listview 的项目中绘制边框?

回答by Digvesh Patel

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

and add property to listview

并将属性添加到列表视图

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

回答by Jitender Dev

Set the background of your list's child element as

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

 android:background="@drawable/shape"

and then in your res>drawable create a new shape.xml

然后在你的 res>drawable 中创建一个新的shape.xml

like this

像这样

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

回答by Md Abdul Gafur

Create an xml drawable such as /res/drawable/textlines.xmland assign this as a Edittext's background property.

创建一个 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. 

回答by GrIsHu

To set the border in ListViewset the property android:divider="@drawable/img_list"and android:dividerHeight="1px"to set the height of the divider.

ListViewset 属性中设置边框android:divider="@drawable/img_list"android:dividerHeight="1px"设置分隔线的高度。

Use the below code to set the border of EditText.

使用以下代码设置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>

So your EditText will look like. You can change the colors as per your needs.

所以你的 EditText 看起来像。您可以根据需要更改颜色。

enter image description here

在此处输入图片说明

回答by vipul mittal

set following shape as the background of you list row item:

将以下形状设置为列表行项目的背景:

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

回答by Ali

I think if you set transcriptMode property of listview it will show the borders too.

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

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

So don't need any shape xml.

所以不需要任何形状xml。