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?
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"
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:
Remove android:gravity="center_horizontal" from main container it makes the children go to the center.
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>
Because LinearLayout
gravity
does not always work as expected, I suggest you make both internal LinearLayout
s 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>
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>
You can use a mix of containers(Layouts).
If you want to put controls in a row but want to anchor it to the bottom you can use the mixture of linear layout
and relative like
this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WebActivity" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
android:background="@color/white"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="backWeb"
android:text="Back />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home" />
<Button
android:onClick="forwardWeb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward" />
</LinearLayout>
</RelativeLayout>
I have a linear view inside a relative layout:
Final result:
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.