简体   繁体   English

android xml布局中的分隔符问题

[英]Divider issue in android xml layout

样品

My goal is to get the layout like the sample about. 我的目标是获得类似于样本的布局。 However, I can't get the small line above the button bar. 但是,我无法获得按钮栏上方的小线。

What I get is like this. 我得到的是这样的。

在此输入图像描述

my xml code is : 我的xml代码是:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginTop="24dp"
    >

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_alignParentTop="true"
      android:layout_above="@+id/buttonBarLayout"
      android:showDividers="middle"
      android:divider="?android:dividerHorizontal">

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Section header"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 1"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 2"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

  </LinearLayout>

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="horizontal"
      style="?android:buttonBarStyle"
      >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Discard"
        style="?android:buttonBarButtonStyle"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save"
        style="?android:buttonBarButtonStyle"/>

  </LinearLayout>

</RelativeLayout>

I believe that the line is the middle divider. 我相信这条线是中间分隔线。 However, if I use LinearLayout the divider will show right under Sample item 2, which is not what I want. 但是,如果我使用LinearLayout,分隔符将显示在示例项2下,这不是我想要的。

So how can I get what the sample shows? 那么我怎样才能得到样本显示的内容?

Add an extra View to your layout which will take no height as below... 在您的布局中添加一个额外的View ,其height不会低于以下...

<View
    android:layout_width="match_parent"
    android:layout_height="0dip" />

Then add the above View and the Linearlayout which holding Buttons inside another LinearLayout with android:divider="?android:dividerHorizontal" attribute as below... 然后添加上面ViewLinearlayout持有本Buttons另一个内部LinearLayoutandroid:divider="?android:dividerHorizontal"属性如下...

<LinearLayout
    android:id="@+id/buttonBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:divider="?android:dividerHorizontal"
    android:orientation="vertical"
    android:showDividers="middle" >

    <View
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:background="#000000" />

    <LinearLayout
        style="?android:buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="?android:dividerHorizontal"
        android:orientation="horizontal" >

        <Button
            style="?android:buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Discard" />

        <Button
            style="?android:buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save" />
    </LinearLayout>

</LinearLayout>

Finally, your layout will be... 最后,你的布局将是......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginTop="24dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttonBarLayout"
        android:layout_alignParentTop="true"
        android:divider="?android:dividerHorizontal"
        android:orientation="vertical"
        android:showDividers="middle" >

        <TextView
            style="?android:listSeparatorTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Section header" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:divider="?android:dividerVertical"
            android:dividerPadding="8dp"
            android:orientation="horizontal"
            android:showDividers="middle" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:text="Sample item 1"
                android:textAppearance="?android:textAppearanceMedium" />

            <ImageButton
                style="?android:borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:divider="?android:dividerVertical"
            android:dividerPadding="8dp"
            android:orientation="horizontal"
            android:showDividers="middle" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:text="Sample item 2"
                android:textAppearance="?android:textAppearanceMedium" />

            <ImageButton
                style="?android:borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttonBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:divider="?android:dividerHorizontal"
        android:orientation="vertical"
        android:showDividers="middle" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dip" />

        <LinearLayout
            style="?android:buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="?android:dividerHorizontal"
            android:orientation="horizontal" >

            <Button
                style="?android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Discard" />

            <Button
                style="?android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Save" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

Resulted Layout: 结果布局:

在此输入图像描述

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginTop="24dp"
    >

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_alignParentTop="true"
      android:layout_above="@+id/buttonBarLayout"
      android:showDividers="middle"
      android:divider="?android:dividerHorizontal">

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Section header"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 1"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 2"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

  </LinearLayout>

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="Vertical"
      style="?android:buttonBarStyle"
      >
      <View android:id="@+id/divider"
        android:layout_widht="match_parent"
        android:layout_height = "1dp"
        android:background="@android:color/black"/>

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
       android:orientation="horizontal"
      style="?android:buttonBarStyle"
      >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Discard"
        style="?android:buttonBarButtonStyle"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save"
        style="?android:buttonBarButtonStyle"/>

  </LinearLayout>
 </LinearLayout>

</RelativeLayout>

check this below layout. 检查下面的布局。 I've made few changes to add the horizontal divider above ButtonBarLayout. 我做了一些更改,在ButtonBarLayout上面添加水平分隔符。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:layout_alignParentTop="true"
  android:layout_above="@+id/buttonBarLayout"
  android:showDividers="middle"
  android:divider="?android:dividerHorizontal"
  android:layout_marginLeft="18dp"
  android:layout_marginRight="18dp"
  android:layout_marginTop="24dp" >

<TextView
    style="?android:listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Section header"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:showDividers="middle"
    android:divider="?android:dividerVertical"
    android:dividerPadding="8dp">

  <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:textAppearance="?android:textAppearanceMedium"
      android:text="Sample item 1"
      android:layout_gravity="center_vertical"/>

  <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="?android:borderlessButtonStyle"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:showDividers="middle"
    android:divider="?android:dividerVertical"
    android:dividerPadding="8dp">

  <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:textAppearance="?android:textAppearanceMedium"
      android:text="Sample item 2"
      android:layout_gravity="center_vertical"/>

  <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="?android:borderlessButtonStyle"/>

</LinearLayout>

</LinearLayout>

<LinearLayout android:id="@+id/buttonBarLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:orientation="vertical"
  style="?android:buttonBarStyle">

  <View
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:background="#F1F1F1" />

  <LinearLayout android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  style="?android:buttonBarStyle">

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Discard"
    style="?android:buttonBarButtonStyle" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Save"
    style="?android:buttonBarButtonStyle" />
</LinearLayout>

You have to give the id to the Linear layout above the button linear layout and place the view in between the button linear layout and linear layout above it. 您必须将id放在按钮线性布局上方的线性布局中,并将视图放在按钮线性布局和其上方的线性布局之间。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginTop="24dp">

  <LinearLayout
      android:id="@+id/layout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_alignParentTop="true"
      android:layout_above="@+id/buttonBarLayout"
      android:showDividers="middle"
      android:divider="?android:dividerHorizontal">

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Section header"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 1"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 2"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

  </LinearLayout>


  <View
       android:id="@+id/view1"
android:layout_width="fill_parent"
 android:layout_height="1dp"
  android:layout_below="@+id/layout1"
 android:background="#000000" />

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"

      android:orientation="horizontal"
      style="?android:buttonBarStyle">



    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Discard"
        style="?android:buttonBarButtonStyle"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save"
        style="?android:buttonBarButtonStyle"/>

  </LinearLayout>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM