简体   繁体   中英

XML in Android Studio: Wrapcontent in RelativeLayout

I need to have items arranged vertically in this order: TextView, LinearLayout (under the textview), then BottomNavigationView. But the result is this:

在此处输入图片说明

I have this code:

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

What am I doing wrong?

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

This comes in the order "TextView, LinearLayout (under the textview)" specified in the question. The android:gravity="centre" feature is used to 'Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.' for the TextView.

Set this to the 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>

ou can make something like this: with property 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM