How to add (vertical) divider to a horizontal LinearLayout?

后端 未结 11 574
有刺的猬
有刺的猬 2020-11-27 11:36

I\'m trying to add a divider to a horizontal linear layout but am getting nowhere. The divider just doesn\'t show. I am a total newbie with Android.

This is my layou

相关标签:
11条回答
  • 2020-11-27 11:56

    Try this, create a divider in the res/drawable folder:

    vertical_divider_1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">    
        <size android:width="1dip" />
        <solid android:color="#666666" />    
    </shape> 
    

    And use the divider attribute in LinearLayout like this:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal"
        android:divider="@drawable/vertical_divider_1"
        android:dividerPadding="12dip"
        android:showDividers="middle"
        android:background="#ffffff" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>
    

    Note: android:divider is only available in Android 3.0 (API level 11) or higher.

    0 讨论(0)
  • 2020-11-27 12:00

    Your divider may not be showing due to too large dividerPadding. You set 22dip, that means the divider is truncated by 22dip from top and by 22dip from bottom. If your layout height is less than or equal 44dip then no divider is visible.

    0 讨论(0)
  • 2020-11-27 12:01

    If the answer of Kapil Vats is not working try something like this:

    drawable/divider_horizontal_green_22.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <size android:width="22dip"/>
        <solid android:color="#00ff00"/>
    
    </shape>
    

    layout/your_layout.xml

    LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/llTopBar"
                android:orientation="horizontal"
                android:divider="@drawable/divider_horizontal_green_22"
                android:showDividers="middle"
               >
    

    I encountered an issue where the padding attribute wasn't working, thus I had to set the height of the divider directly in the divider.

    Note:

    If you want to use it in vertical LinearLayout, make a new one, like this: drawable/divider_vertical_green_22.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <size android:height="22dip"/>
        <solid android:color="#00ff00"/>
    
    </shape>
    
    0 讨论(0)
  • 2020-11-27 12:02

    You can use the built in divider, this will work for both orientations.

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="?android:attr/listDivider"
      android:orientation="horizontal"
      android:showDividers="middle">
    
    0 讨论(0)
  • 2020-11-27 12:05

    Update: pre-Honeycomb using AppCompat

    If you are using the AppCompat library v7 you may want to use the LinearLayoutCompat view. Using this approach you can use drawable dividers on Android 2.1, 2.2 and 2.3.

    Example code:

    <android.support.v7.widget.LinearLayoutCompat
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:showDividers="middle"
            app:divider="@drawable/divider">
    

    drawable/divider.xml: (divider with some padding on the top and bottom)

    <?xml version="1.0" encoding="UTF-8"?>
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
            android:insetBottom="2dp"
            android:insetTop="2dp">
        <shape>
            <size android:width="1dp" />
            <solid android:color="#FFCCCCCC" />
        </shape>
    </inset>
    

    Very important note: The LinearLayoutCompat view does not extend LinearLayout and therefor you should not use the android:showDividers or android:divider properties but the custom ones: app:showDividers and app:divider. In code you should also use the LinearLayoutCompat.LayoutParams not the LinearLayout.LayoutParams!

    0 讨论(0)
提交回复
热议问题