Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使子视图组向下填充父视图组的其余部分_Android_Xml_Android Layout_Android Linearlayout_Android Relativelayout - Fatal编程技术网

Android 使子视图组向下填充父视图组的其余部分

Android 使子视图组向下填充父视图组的其余部分,android,xml,android-layout,android-linearlayout,android-relativelayout,Android,Xml,Android Layout,Android Linearlayout,Android Relativelayout,考虑这样一种情况,即需要线性布局中的视图来填充布局中剩余的空白空间;然后,其中一个将此视图的权重设置为1,而其他视图的权重为0 是否有任何方法可以在父视图组中对子视图组执行类似的操作 例如,考虑下面的XML: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_he

考虑这样一种情况,即需要线性布局中的视图来填充布局中剩余的空白空间;然后,其中一个将此视图的权重设置为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>

您将看到,在几个文本视图下面,我插入了一个子LinearLayout

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


有什么方法可以做到这一点吗?

使用
RelativeLayout
是不可能的,但您可以将其转换为
ConstraintLayout
并使用
app:constraintVerticalWeight=“1”
来控制主编辑文本组件的高度

事实上,您可以完全取消嵌套的
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>


嘿,我注意到您添加了一些类似的属性:“tools:layout\u editor\u absoluteX=“-16dp””您能用初学者的术语解释一下这个属性的作用吗?我正在努力找到一个简洁的答案。不,我没有添加这些属性。我没有在回答中编辑代码段,也看不到对工具名称空间或那些属性的任何引用。。我想Android Studio一定是在您转换布局时自动添加的。请参阅,以获取有关其工作的信息。