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

后端 未结 11 573
有刺的猬
有刺的猬 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:47

    I just ran into the same problem today. As the previous answers indicate, the problem stems from the use of a color in the divider tag, rather than a drawable. However, instead of writing my own drawable xml, I prefer to use themed attributes as much as possible. You can use the android:attr/dividerHorizontal and android:attr/dividerVertical to get a predefined drawable instead:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:showDividers="middle"
        android:divider="?android:attr/dividerVertical"
        android:orientation="horizontal">
        <!-- other views -->
    </LinearLayout>
    

    The attributes are available in API 11 and above.

    Also, as mentioned by bocekm in his answer, the dividerPadding property does NOT add extra padding on either side of a vertical divider, as one might assume. Instead it defines top and bottom padding and thus may truncate the divider if it's too large.

    0 讨论(0)
  • 2020-11-27 11:48

    Frustratingly, you have to enable showing the dividers from code in your activity. For example:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Set the view to your layout
        setContentView(R.layout.yourlayout);
    
        // Find the LinearLayout within and enable the divider
        ((LinearLayout)v.findViewById(R.id.llTopBar)).
            setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
    
    }
    
    0 讨论(0)
  • 2020-11-27 11:49

    You have to create the any view for separater like textview or imageview then set the background for that if you have image else use the color as the background.

    Hope this helps you.

    0 讨论(0)
  • 2020-11-27 11:53

    use this for horizontal divider

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@color/honeycombish_blue" />
    

    and this for vertical divider

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/honeycombish_blue" />
    

    OR if you can use the LinearLayout divider, for horizontal divider

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

    and in LinearLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@drawable/divider"
        android:orientation="vertical"
        android:showDividers="middle" >
    

    If you want to user vertical divider then in place of android:height="1dp" in shape use android:width="1dp"

    Tip: Don't forget the android:showDividers item.

    0 讨论(0)
  • 2020-11-27 11:53

    It is easy to add divider to layout, we don't need a separate view.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:divider="?android:listDivider"
        android:dividerPadding="2.5dp"
        android:orientation="horizontal"
        android:showDividers="middle"
        android:weightSum="2" ></LinearLayout>
    

    Above code make vertical divider for LinearLayout

    0 讨论(0)
  • 2020-11-27 11:53

    In order to get drawn, divider of LinearLayout must have some height while ColorDrawable (which is essentially #00ff00 as well as any other hardcoded color) doesn't have. Simple (and correct) way to solve this, is to wrap your color into some Drawable with predefined height, such as shape drawable

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