【问题标题】:Make child view group fill the rest of the parent group downwards使子视图组向下填充父组的其余部分
【发布时间】:2020-03-15 11:31:37
【问题描述】:

考虑当一个人想要一个线性布局中的视图来填充布局中剩余的空白空间的情况;然后将这个视图的权重设置为 1,而其他视图的权重为 0。

有没有办法对父视图组中的子视图组做类似的事情。

例如,考虑下面我的 xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/coffee"
            android:scaleType="centerCrop"
            android:alpha="0.7"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="@string/bus_title"
            android:textColor="#FFFF"
            android:textSize="24dp"
            android:textStyle="bold|italic" />

        <TextView
            android:id="@+id/about_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="About Us"
            android:textColor="#FFFF"
            android:textSize="16sp"
            android:textStyle="bold|italic" />

        <TextView
            android:id="@+id/about_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/about_heading"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="@string/bus_description"
            android:textColor="#FFFF"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/opening_hours_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/about_text"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="@string/opening_hours_header"
            android:textColor="#FFFF"
            android:textSize="16sp"
            android:textStyle="bold|italic" />

        <TextView
            android:id="@+id/opening_times"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/opening_hours_header"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="@string/opening_hours"
            android:textColor="#FFFF"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/Contact_Us_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/opening_times"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:text="@string/Contact_heading"
            android:textColor="#FFFF"
            android:textSize="16sp"
            android:textStyle="bold|italic" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical"
            android:layout_below="@id/Contact_Us_heading">

            <EditText
                android:id="@+id/email_field"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#80E0E0E0"
                android:gravity="top"
                android:inputType="text"
                android:hint="Type a message here..."
                android:layout_below="@id/Contact_Us_heading"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="SEND" />

        </LinearLayout>

    </RelativeLayout>

</ScrollView>

你会看到我在几个 textViews 下面插入了一个子 LinearLayout。

我希望线性布局填充相对布局下方的“剩余空间”,就像 editText 视图填充线性布局的其余部分一样。

有什么办法吗?

【问题讨论】:

    标签: android xml android-layout android-linearlayout android-relativelayout


    【解决方案1】:

    RelativeLayout 不可能,但您可以将其转换为 ConstraintLayout 并使用 app:constraintVerticalWeight="1" 来控制主 EditText 组件的高度。

    事实上,您可以完全取消嵌套的 LinearLayout,因为 ConstraintLayout 可以一次性为您完成所有操作!

    修改后的布局 XML 如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.7"
                android:scaleType="centerCrop"
                android:src="@drawable/coffee" />
    
            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="@string/bus_title"
                android:textColor="#FFFF"
                android:textSize="24dp"
                android:textStyle="bold|italic"
                app:layout_constraintBottom_toTopOf="@+id/about_heading"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/about_heading"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="About Us"
                android:textColor="#FFFF"
                android:textSize="16sp"
                android:textStyle="bold|italic"
                app:layout_constraintBottom_toTopOf="@+id/about_text"
                app:layout_constraintTop_toBottomOf="@id/title"
                app:layout_constraintVertical_bias="0" />
    
            <TextView
                android:id="@+id/about_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="@string/bus_description"
                android:textColor="#FFFF"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/opening_hours_header"
                app:layout_constraintTop_toBottomOf="@+id/about_heading" />
    
            <TextView
                android:id="@+id/opening_hours_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="@string/opening_hours_header"
                android:textColor="#FFFF"
                android:textSize="16sp"
                android:textStyle="bold|italic"
                app:layout_constraintBottom_toTopOf="@+id/opening_times"
                app:layout_constraintTop_toBottomOf="@+id/about_text" />
    
            <TextView
                android:id="@+id/opening_times"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="@string/opening_hours"
                android:textColor="#FFFF"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/Contact_Us_heading"
                app:layout_constraintTop_toBottomOf="@+id/opening_hours_header" />
    
            <TextView
                android:id="@+id/Contact_Us_heading"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="@string/Contact_heading"
                android:textColor="#FFFF"
                android:textSize="16sp"
                android:textStyle="bold|italic"
                app:layout_constraintBottom_toTopOf="@+id/email_field"
                app:layout_constraintTop_toBottomOf="@+id/opening_times" />
    
            <EditText
                android:id="@+id/email_field"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="#80E0E0E0"
                android:hint="Type a message here..."
                android:inputType="text"
                app:layout_constraintBottom_toTopOf="@+id/send_button"
                app:layout_constraintTop_toBottomOf="@id/Contact_Us_heading"
                app:layout_constraintVertical_weight="1" />
    
            <Button
                android:id="@+id/send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SEND"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/email_field" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </ScrollView>
    

    在 AndroidStudio UI 设计器中生成的布局如下所示(忽略缺少的字符串/可绘制定义,并注意我将文本颜色更改为黑色,以便我们可以看到文本视图的位置):

    【讨论】:

    • 嘿,我注意到你添加了一些类似这样的属性:'tools:layout_editor_absoluteX="-16dp"' 你能用初学者的术语解释一下这个属性的作用吗?我正在努力寻找一个简洁的答案。
    • 不,我没有添加这些属性。我没有在我的答案中编辑代码 sn-p 并且看不到对工具命名空间或这些属性的任何引用。我认为 Android Studio 必须在您转换布局时自动添加它们。请参阅this answer 了解他们的工作信息。
    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2016-02-13
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多