androidxml布局中的分隔符问题
我的目标是获得与示例类似的布局。然而,我无法获得按钮栏上方的小线条 我得到的是这样的 我的xml代码是:androidxml布局中的分隔符问题,android,xml,layout,divider,buttonbar,Android,Xml,Layout,Divider,Buttonbar,我的目标是获得与示例类似的布局。然而,我无法获得按钮栏上方的小线条 我得到的是这样的 我的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="m
<?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>
我相信这条线是中间的分隔线。但是,如果我使用LinearLayout,分隔符将显示在示例项2的正下方,这不是我想要的
那么,我如何才能获得示例显示的内容呢?在布局中添加一个额外的
视图
,该视图不需要高度
,如下所示
<View
android:layout_width="match_parent"
android:layout_height="0dip" />
<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>
最后,您的布局将是
<?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>
结果布局:
<?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>
检查下面的布局。我对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>
您必须为按钮线性布局上方的线性布局指定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>
您可以使用
在两个按钮之间添加分隔符。@Owen Zhao:Hamid Shatu提供的答案没有完全填充分隔符宽度。它在左右两边都留下了一些空白。加上宽度设置为0,使其不可见。