简体   繁体   中英

How to bottom-align my button bar in Android LinearLayout

I'm working in a master-detail app, and my activity_twopane has a button bar (which I would like to place) at the bottom of the details pane. I can place it under the fragments detail, but I can't get it to align to the bottom no matter what.
I have it working fine in single pane mode, using android:layout_weight="1" , but even with the warning for using nested weights, it doesn't work in two pane mode.
This is what it looks like now:
目前的布局
And this is the layout xml I've got so far:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".DocumentoListActivity" >

    <fragment
        android:id="@+id/documento_list"
        android:name="br.com.DocumentoListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="3"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/documento_detail_container"
            android:layout_width="match_parent"
            android:layout_height="fill_parent" />

        <LinearLayout
            android:id="@+id/documento_twopane_buttons"
            style="android:attr/buttonBarStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/documento_twopane_buttonBaixar"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1" />

            <Button
                android:id="@+id/documento_twopane_buttonAssinar"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 2" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>  

As my button bar is inside a LinearLayout , I can't use a RelativeLayout with android:layout_alignParentBottom="true" in it.

Question

Is this doable with LinearLayout s or do I have to change everything and put it into a RelativeLayout ??

You can use RelativeLayout here, but nested layouts are not a big deal when used this way. It will only be inflated once. Your Detail LinearLayout should look like this:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_weight="3"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/documento_detail_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1" />

    <LinearLayout
        android:id="@+id/documento_twopane_buttons"
        style="android:attr/buttonBarStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/documento_twopane_buttonBaixar"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:id="@+id/documento_twopane_buttonAssinar"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2" />
    </LinearLayout>
</LinearLayout>

The important thing to note here is that you don't have to use android:weight on all children in a LinearLayout . If you have 2 children, and set the main one's weight to 1, with 0 as the corresponding length or width axis, you can use wrap_content with the other child, and it will only take up as much space as is needed.

Wrap your buttons Linear layout inside another layout with height set to match_parent

Set the height of you Linear layout (containing buttons) to match_parent , and set its gravity to bottom

This works as you wish

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginLeft="16dp"
 android:layout_marginRight="16dp"
 android:baselineAligned="false"
 android:orientation="horizontal"
 android:showDividers="middle"
 tools:context=".DocumentoListActivity" >

<fragment
android:id="@+id/documento_list"
android:name="br.com.DocumentoListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />

<!-- Set this layout height to match_parent -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >

 <!-- Set this layout height to wrap_content -->
<FrameLayout
    android:id="@+id/documento_detail_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 <!-- Set this layout height to match_parent -->
<LinearLayout
    android:id="@+id/documento_twopane_buttons"
    style="android:attr/buttonBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<!-- Set this layout height to match_parent and gravity to bottom -->
<LinearLayout
    android:id="@+id/documento_twopane_buttons1"
    style="android:attr/buttonBarStyle"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:baselineAligned="false"
    android:gravity="bottom"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/documento_twopane_buttonBaixar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/documento_twopane_buttonAssinar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Even though @twntee's answer seemed like it worked fine, if text on the right pane was too long, it ended up on top of the buttons, making them unreachable. So I had to make a few switches. So now I had to make use of nested weights, but at least there was no need for those nested LinearLayouts :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".DocumentoListActivity" >

    <fragment
        android:id="@+id/documento_list"
        android:name="br.com.DocumentoListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/documento_detail_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            tools:context=".DocumentoDetailActivity"
            tools:ignore="MergeRootFrame,NestedWeights" />

        <LinearLayout
            android:id="@+id/documento_twopane_buttons"
            style="android:attr/buttonBarStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/documento_twopane_buttonOne"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_one" />

            <Button
                android:id="@+id/documento_twopane_buttonTwo"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_two" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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