在 Android Studio 相对布局中正确添加标题

Adding a title properly in Android Studio Relative Layout

我现在做一个聊天应用程序,我想给聊天室一个我要写信给的人的头衔。问题是我不知道如何在我的聊天布局中正确对齐它而不让我的文本视图进入列表视图本身。这是我的 xml 文件。

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="il.co.appschool.firebasechatapp.ChatActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/tvName"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_send_black_24dp"
        android:id="@+id/fab"
        android:tint="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:fabSize="mini" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/fab"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Input"
            android:id="@+id/input"
            />
    </android.support.design.widget.TextInputLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_alignParentStart="false"
        android:layout_above="@id/fab"
        android:dividerHeight="16dp"
        android:divider="@android:color/transparent"
        android:id="@+id/list_of_messages"
        android:layout_marginBottom="16dp"/>
</RelativeLayout>

如您所见,我的文本视图位于文件的顶部,但我无法对齐它,因此它不会进入我的列表视图。你能帮我告诉我写什么才能让它看起来不错吗?

试试这个 在你的 TextView [=13= 中使用 android:layout_alignParentTop="true" ] 在你的 ListView

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="il.co.appschool.firebasechatapp.ChatActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="dsd"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/tvName"
        android:layout_alignParentTop="true"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_send_black_24dp"
        android:id="@+id/fab"
        android:tint="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:fabSize="mini" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/fab"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Input"
            android:id="@+id/input"
            />
    </android.support.design.widget.TextInputLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_alignParentStart="false"
        android:layout_above="@id/fab"
        android:dividerHeight="16dp"
        android:divider="@android:color/transparent"
        android:id="@+id/list_of_messages"
        android:layout_below="@+id/tvName"
        android:layout_marginBottom="16dp"/>
</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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="il.co.appschool.firebasechatapp.ChatActivity">
    <TextView
        android:id="@+id/sampleText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/tvName"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_send_black_24dp"
        android:id="@+id/fab"
        android:tint="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:fabSize="mini" />

    <android.support.design.widget.TextInputLayout
 `enter code here`android:layout_below="@+id/sampleText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/fab"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Input"
            android:id="@+id/input"
            />
    </android.support.design.widget.TextInputLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_alignParentStart="false"
        android:layout_above="@id/fab"
        android:dividerHeight="16dp"
        android:divider="@android:color/transparent"
        android:id="@+id/list_of_messages"
        android:layout_marginBottom="16dp"/>
</RelativeLayout>

添加 属性 layout_below 到 ListView 如下:

  <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="false"
    android:layout_alignParentStart="false"
    android:layout_above="@id/fab"
    android:dividerHeight="16dp"
    android:divider="@android:color/transparent"
    android:id="@+id/list_of_messages"
    android:layout_below="@+id/tvName"
    android:layout_marginBottom="16dp"/>

这行得通。谢谢

我没有正确理解你的问题,但是你可以做的一件事是将你的 ListView 放在 TextView 下面,就是在 ListView 中使用 android:layout_below 属性。