Android 在屏幕底部保持线性布局

Android 在屏幕底部保持线性布局,android,xaml,xamarin,Android,Xaml,Xamarin,我知道这个问题被问了很多,因为我在这里尝试了很多例子,但我就是不能让它工作,这就是我所拥有的: <?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:orientat

我知道这个问题被问了很多,因为我在这里尝试了很多例子,但我就是不能让它工作,这就是我所拥有的:

<?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>
其中一个问题可能是,我正在动态地填充单选组中的单选按钮,因此有时可能是4个单选按钮,而在另一个时刻可能是1个,我不知道这是否会影响我正在尝试的任何操作


我要做的是保持第一个线性层在顶部,第二个线性层在按钮上。我做错了什么?

首先要做的是将主容器的高度作为匹配的父容器,然后将视图扩展为设备屏幕的高度,接下来,我无法理解为什么您的第一个子LinearLayout将0dp设置为android:layout\u height,但您应该将其设置为android:layout\u height=wrap\u内容,最后要做的是将android:layout\u gravity=bottom设置为第二个子LinearLayout

另外,我建议阅读android:layout_gravity=和android:gravity=的区别,它们都是LinearLayout属性

更新:

从主容器中移除android:gravity=水平居中 让孩子们去中心

从中删除android:gravity=bottom android:layout\u weight=1 第一个孩子你为什么要把第一个线性布局转到 底部

下面是一些干净的代码:

<?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>
因为LinearLayout gravity并不总是像预期的那样工作,我建议您使两个内部LinearLayout都具有android:layout_height=wrap_内容,并插入一个垂直拉伸的空视图,并填充顶部和底部内容之间的所有可用空间

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

请下次提供更好的描述你到底想要什么。我不确定,但也许你想要这个。更改根LinearLayout高度以匹配父对象,并删除第一个LinearLayout android:gravity=bottom


您可以使用布局和重量。80和20,它将在所有屏幕上工作

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