在android中創(chuàng)建布局時(shí)骆莹,發(fā)現(xiàn)有些控件之間加一些分割線,會(huì)很美觀靶草,上網(wǎng)搜索了下蹄胰,找到了三種方式創(chuàng)建分割線,下面就來(lái)分別來(lái)試一下奕翔。
1. 使用View
也是最簡(jiǎn)單的一種方式裕寨,直接定義寬度和高度,設(shè)置顏色即可。
但是宾袜,分割線較多的布局中捻艳,這種不太適合,會(huì)占用較多內(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
自定義一個(gè)分割線的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í)月培,一般在垂直布局中設(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>
注意點(diǎn):
(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í)方式是相同的,只不過改變一下寬度和高度灭袁,高度匹配父布局猬错,寬度設(shè)置為線寬,這里僅View的方式為例,在兩個(gè)TextView之間加入一個(gè)分割線简卧。
<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 是較為簡(jiǎn)單的方式析校;
(2)若分割線數(shù)量較多,可以采用 3铜涉,能夠較少布局所占內(nèi)存智玻,并較少布局中控件的數(shù)量,達(dá)到復(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>