繁体   English   中英

如何在Android LinearLayout中底部对齐我的按钮栏

[英]How to bottom-align my button bar in Android LinearLayout

我正在使用主 - 详细信息应用程序,我的activity_twopane在详细信息窗格的底部有一个按钮栏(我想放置它)。 我可以将它放在碎片细节下面,但无论如何我都无法将它对齐到底部。
我在单窗格模式下工作正常,使用android:layout_weight="1" ,但即使有使用嵌套权重的警告,它也无法在两种窗格模式下工作。
这就是现在的样子:
目前的布局
这是我到目前为止的布局xml:

<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>  

由于我的按钮栏在LinearLayout ,我不能在其中使用RelativeLayout with android:layout_alignParentBottom="true"RelativeLayout with android:layout_alignParentBottom="true"

这是可行的LinearLayout s还是我必须更改所有内容并将其放入RelativeLayout

你可以在这里使用RelativeLayout ,但是这样使用嵌套布局并不是什么大问题。 它只会膨胀一次。 您的详细信息LinearLayout应如下所示:

<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>

这里需要注意的重要一点是,您不必对LinearLayout所有子项使用android:weight 如果您有2个子wrap_content ,并将主wrap_content的权重设置为1,并将0作为相应的长度或宽度轴,则可以将wrap_content与另一个子wrap_content一起使用,并且只占用所需的空间。

将您的按钮包裹在另一个布局中的线性布局,其高度设置为match_parent

将线性布局(包含按钮)的高度设置为match_parent ,并将其重力设置为bottom

这可以按照您的意愿工作

 <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>

虽然@ twntee的回答看起来好像很好,如果右侧窗格上的文字太长,它最终会出现在按钮顶部,使它们无法访问。 所以我不得不做几个开关。 所以现在我必须使用嵌套权重,但至少不需要那些嵌套的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>

暂无
暂无

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

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