Keep LinearLayout at the bottom of screen

十年热恋 提交于 2020-03-06 10:03:16

问题


I know this question is asked a lot, because I was trying a lot of examples posted in here, but I just Can't make it work, this is what I have:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="#3F51B5"
    android:padding="5dp"
    android:paddingBottom="500dp">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/linearLayout2"
        android:gravity="bottom"
        android:layout_weight="1">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnTarjeta"
            android:background="@color/my_purple"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp" />
        <TextView
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/my_white"
            android:id="@+id/textView1"
            android:fontFamily="sans-serif-medium"
            android:layout_marginBottom="10dp" />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioGp"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_marginBottom="10dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3"
        android:layout_gravity="bottom">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1" />
    </LinearLayout>
</LinearLayout>

One of the things that might be a "problem" is that I'm dynamically filling the radio buttons inside the radio group, so sometimes can be 4 radio buttons and in an other moment can be 1, I don't know if this affect any of what I'm trying to do.

And what I'm trying to do is keep the the first linear layer at the top, and the second linear layer at button. What I'm doing wrong?


回答1:


Please, next time provide better description of what you exactly want. I'm not sure, but maybe you want this. Change your root LinearLayout height to match_parent, and remove first LinearLayout android:gravity="bottom"

Screenshot of Android Studio Preview




回答2:


Because LinearLayout gravity does not always work as expected, I suggest you make both internal LinearLayouts to have android:layout_height="wrap_content" and insert an empty View that will stretch vertically and fill all available space between the top and bottom content.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_content"
    ... >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2">
        <Button ... />
        <TextView ... />
        <RadioGroup .../>
    </LinearLayout>

    <View 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3">
        <Button ... />
        <Button ... />
    </LinearLayout>
</LinearLayout>



回答3:


First thing to do you have to put the height to your main container as match_parent with that you will expand the view as the height of the device screen, next I can't get the logic why your first child - LinearLayout has 0dp as android:layout_height but you should put it to android:layout_height="wrap_content" and the last thing to do is to put android:layout_gravity="bottom" to the second child LinearLayout.

Also I suggest to read for the difference of android:layout_gravity="" and android:gravity="" which are both LinearLayout properties.

Update:

  1. Remove android:gravity="center_horizontal" from main container it makes the children go to the center.

  2. Remove android:gravity="bottom" android:layout_weight="1" from first child why are you teling the first linear layout to go to the bottom?

Here is some clean code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3F51B5"
    android:padding="20dp">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnTarjeta"
            android:background="@color/my_purple"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp" />
        <TextView
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/my_white"
            android:id="@+id/textView1"
            android:fontFamily="sans-serif-medium"
            android:layout_marginBottom="10dp" />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioGp"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_marginBottom="10dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3"
        android:layout_gravity="bottom">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1" />
    </LinearLayout>
</LinearLayout>



回答4:


you can use layout_weight. 80 and 20 , it will work on all screens

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#3F51B5"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="5dp"
android:paddingBottom="500dp"
android:weightSum="100">

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="80"
    android:gravity="bottom"
    android:minHeight="25px"
    android:minWidth="25px"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnTarjeta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@color/my_purple"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:fontFamily="sans-serif-medium"
        android:text="Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/my_white" />

    <RadioGroup
        android:id="@+id/radioGp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:minHeight="25px"
        android:minWidth="25px" />
</LinearLayout>



  <LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="bottom"
    android:layout_weight="20"
    android:orientation="vertical">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

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


来源:https://stackoverflow.com/questions/46354035/keep-linearlayout-at-the-bottom-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!