在android中創(chuàng)建布局時,發(fā)現(xiàn)有些控件之間加一些分割線,會很美觀,上網(wǎng)搜索了下,找到了三種方式創(chuàng)建分割線,下面就來分別來試一下。
1. 使用View
也是最簡單的一種方式,直接定義寬度和高度,設(shè)置顏色即可。
但是,分割線較多的布局中,這種不太適合,會占用較多內(nèi)存
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#303F9F"/>
2. 使用ImageView
方法與View類似,也是設(shè)置高度、寬度和顏色即可
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorAccent"/>
3. 自定義xml
自定義一個分割線的divider.xml,放置drawable目錄下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent"/>
<size android:height="1dp"/>
</shape>
使用時,一般在垂直布局中設(shè)置,水平布局中不能顯示
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:divider="@drawable/divider"
android:showDividers="end"
android:dividerPadding="1.5dp">
...
</LinearLayout>
注意點:
(1)垂直布局
(2)android:divider="@drawable/divider" ,不能直接設(shè)置顏色,否則不顯示,divider就是自定義的xml
(3)android:showDividers="end" 設(shè)置顯示位置
end:末端
beginning:前端
middle:中間
none:不顯示
(4)android:dividerPadding="1.5dp",可以更改分割線的寬度
4.垂直分割線
有的童鞋可能需要使用在水平的布局中使用分割線,那么如何創(chuàng)建呢?
其實方式是相同的,只不過改變一下寬度和高度,高度匹配父布局,寬度設(shè)置為線寬,這里僅View的方式為例,在兩個TextView之間加入一個分割線。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="Android2"
android:textSize="18sp"/>
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textSize="18sp"
android:text="Android21"/>
</LinearLayout>
總結(jié)
(1)以上就是分割線的三種創(chuàng)建方式,需要根據(jù)自己的布局選擇合適方式,若分割線使用數(shù)量不多,1 和 2 是較為簡單的方式;
(2)若分割線數(shù)量較多,可以采用 3,能夠較少布局所占內(nèi)存,并較少布局中控件的數(shù)量,達到復(fù)用的效果!
附效果及代碼
效果圖布局代碼如下,需要的小伙伴可以試試額!

分割線效果圖
<?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:padding="10dp"
android:divider="@drawable/divider"
android:showDividers="">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="Android1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="button1"/>
</LinearLayout>
<!--方式一:ImageView-->
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorAccent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="Android2"
android:textSize="18sp"/>
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textSize="18sp"
android:text="Android21"/>
</LinearLayout>
<!--方式二:使用View-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#303F9F"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
android:divider="@drawable/divider"
android:showDividers="end"
android:dividerPadding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="Android3"
android:textSize="18sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="button3"/>
</LinearLayout>
<!--方式二:使用View-->
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#303F9F"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="Android4"
android:textSize="18sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="button4"/>
</LinearLayout>
</LinearLayout>