Android adds dividing line divider in LinearLayout

How to add divider line in LinearLayout like this picture below.

 

Recommended method:

LinearLayout has two properties

1、Android:divider="@drawable"

The drawable can be a picture file or a shape drawn by xml.

When using shape, be sure to add <size>, be sure to add color, even if it is transparent, for example:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <!-- The actual line displayed -->
    <stroke
        android:width="1dp"
        android:color="@color/graya"  />
        <!--android:dashGap="2dp"-->
        <!--android:dashWidth="4dp"-->

    <!-- the height of the shape-->
    <size android:height="2dp"/>
</shape>

 2、android:showDividers = "middle|end|beginning|none"

 

middle adds a dividing line in the middle of each item

end adds a dividing line to the last item of the whole

beginning Add a dividing line at the top of the whole

none none

Adding these two properties can achieve the effect in the picture.

However, this property is only supported above Android 3.0.

Compatible with versions below 3.0, need to refer to android-support-v7-appcompat.jar

Use android.support.v7.widget.LinearLayoutCompat, the same usage as LinearLayout. But when you set the attribute again, you need to use the set xml namespace

 比如xmlns:app="http://schemas.android.com/apk/res-auto" app:divider=""

 

Other method one:

Add a View in the middle of each item, set the width and height of the view, and the background. (Not recommended, waste of resources)

 

<View  
        android:layout_width="fill_parent"  
        android:layout_height="2dip"  
        android:background="#FF909090" />  

 

Other method two:

Add an underlined background to each item. (not recommended, troublesome)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326274532&siteId=291194637