【问题标题】:Keep LinearLayout at the bottom of screen将 LinearLayout 保持在屏幕底部
【发布时间】:2017-09-21 22:24:27
【问题描述】:

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

<?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,我不知道如果这会影响我正在尝试做的任何事情。

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

【问题讨论】:

    标签: android xaml xamarin


    【解决方案1】:

    请下次更好地描述您真正想要的东西。我不确定,但也许你想要这个。把你的根LinearLayoutheight改成match_parent,然后先删除LinearLayoutandroid:gravity="bottom"

    Screenshot of Android Studio Preview

    【讨论】:

      【解决方案2】:

      因为LinearLayout gravity 并不总是按预期工作,我建议您将两个内部LinearLayouts 都设置为android:layout_height="wrap_content" 并插入一个空的View,它将垂直拉伸并填充所有可用空间顶部和底部的内容。

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

        首先你必须把你的主容器的高度设置为match_parent,这样你就可以将视图扩展为设备屏幕的高度,接下来我无法理解为什么你的第一个孩子 - LinearLayout有 0dp 为android:layout_height,但你应该把它放到android:layout_height="wrap_content",最后要做的就是把android:layout_gravity="bottom" 放到第二个子LinearLayout。

        另外我建议阅读 android:layout_gravity="" 和 android:gravity="" 这两个 LinearLayout 属性的区别。

        更新:

        1. 从主容器中移除 android:gravity="center_horizo​​ntal" 让孩子们去中心。

        2. 删除 android:gravity="bottom" android:layout_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>
        

        【讨论】:

          【解决方案4】:

          你可以使用 layout_weight。 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>
          

          【讨论】:

            猜你喜欢
            • 2013-01-24
            • 2017-09-13
            • 2013-09-15
            • 1970-01-01
            • 2016-07-23
            • 2012-01-30
            • 2015-02-11
            • 2020-10-13
            • 2016-03-27
            相关资源
            最近更新 更多