简体   繁体   English

Android:LinearLayout底部有2个按钮

[英]Android : LinearLayout with 2 buttons at the bottom

I would like to set my linearlayout with 2 buttons side by side at the bottom of the view. 我想在视图底部并排设置2个按钮的linearlayout。

Today, with my code, the linearlayout stays at the top :( 今天,使用我的代码,线性布局保持在顶部:(

Where is my mistake ? 我的错误在哪里?

<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:background="#0099CC"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="bottom" >

         <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="5dp"
             android:layout_marginRight="5dp"
             android:layout_weight="1"
             android:orientation="vertical" >

             <Button
                 android:id="@+id/startact_btn_connect"
                 style="?android:attr/borderlessButtonStyle"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="10dp"
                 android:background="@drawable/bg_blue_button_login"
                 android:text="Se connecter"
                 android:textColor="@color/blanc" />

             <View
                 android:layout_width="fill_parent"
                 android:layout_height="3dp"
                 android:layout_marginBottom="10dp"
                 android:background="@color/blue_pressed" />
         </LinearLayout>

         <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="5dp"
             android:layout_marginRight="5dp"
             android:layout_weight="1"
             android:orientation="vertical" >

             <Button
                 android:id="@+id/startact_btn_view_annonces"
                 style="?android:attr/borderlessButtonStyle"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="10dp"
                 android:background="@drawable/bg_blue_dark_button_login"
                 android:text="Voir les annonces"
                 android:textColor="@color/blanc" />

             <View
                 android:layout_width="fill_parent"
                 android:layout_height="3dp"
                 android:layout_marginBottom="10dp"
                 android:background="@color/blue_dark_pressed" />
         </LinearLayout>

     </LinearLayout>

</LinearLayout>

Set android:layout_height="match_parent" to the first child of LinearLayout android:layout_height="match_parent"设置为LinearLayout的第一个子节点

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="top"
         android:gravity="bottom" >

         <!-- content -->

     </LinearLayout>

</LinearLayout>

Change the following LinearLayout's height to match_parent: 将以下LinearLayout的高度更改为match_parent:

 <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="bottom" >  

That said, you can also remove the nested linear layouts, and specify orientation:horizontal in the one mentioned above. 也就是说,您也可以删除嵌套的线性布局,并在上面提到的一个中指定方向:水平。 Considering what you need the "view"(s) for, which are within the two nested layouts. 考虑您需要的“视图”,它们位于两个嵌套布局中。

That would simplify your code and layout, something like: 这将简化您的代码和布局,例如:

<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:background="#0099CC"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingTop="20dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/startact_btn_connect"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:text="Se connecter" />

        <Button
            android:id="@+id/startact_btn_view_annonces"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:text="Voir les annonces" />
    </LinearLayout>

</LinearLayout>

try, 尝试,

android:layout_gravity="bottom" 机器人:layout_gravity = “底部”

The key is that you have the orientation set to "vertical". 关键是您将方向设置为“垂直”。 This stacks the view on top of one another. 这将视图堆叠在一起。 To achieve the effect your looking for you'll want to set the orientation to "horizontal". 为了达到效果,您需要将方向设置为“水平”。

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

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