XML 在 Android Studio 中:在 RelativeLayout 中包装内容

XML in Android Studio: Wrapcontent in RelativeLayout

我需要按以下顺序垂直排列项目:TextView、LinearLayout(在 textview 下),然后是 BottomNavigationView。但是结果是这样的:

我有这个代码:

<?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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:textSize="30dp"
            android:gravity="center"
            android:layout_alignParentTop="true"
            android:text="Home"
            />

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:id="@+id/fragmentholder"
            android:orientation="vertical"></LinearLayout>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"
            android:background="?android:attr/windowBackground"

            app:menu="@menu/navigation"/>

</RelativeLayout>

我做错了什么?

将此设置为 LinearLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/message"
    android:layout_above="@+id/navigation"
    android:id="@+id/fragmentholder"
    android:orientation="vertical">
 </LinearLayout>

你可以做这样的事情:使用 属性 layout_below

<?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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_alignParentTop="true"
            android:text="Home"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/message"
            android:id="@+id/fragmentholder"
            android:orientation="vertical"></LinearLayout>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/navigation"/>

</RelativeLayout>
<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Home"
        android:textSize="30dp" />

    <LinearLayout
        android:id="@+id/fragmentholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/message"
        android:orientation="vertical"></LinearLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"

        app:menu="@menu/navigation" />

</RelativeLayout>

这是按照问题中指定的顺序 "TextView, LinearLayout (under the textview)" 出现的。 android:gravity="centre" 功能用于 'Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.' TextView。