Android 布局中分割線創(chuàng)建的三種方式

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,848評論 25 709
  • RelativeLayout 第一類:屬性值為true可false android:layout_centerHr...
    兀兀沙彌閱讀 3,129評論 0 15
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,311評論 0 17
  • 忘了哪一年開始,每到這個季節(jié),我的家鄉(xiāng)名字都會在網(wǎng)絡(luò)電視各種新聞中上榜,然而并不是什么值得自豪的事。 小霾轉(zhuǎn)大霾,...
    蘇麗珍閱讀 272評論 0 0

友情鏈接更多精彩內(nèi)容