简体   繁体   中英

Android - Positioning TextView in RelativeLayout

I'm making a small app for Android and I have a problem with the XAML.

This is the XML layout code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".reverse_screen"
    android:textAllCaps="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <EditText
            android:text="Enter a message"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:id="@+id/hi_field"
            android:layout_height="wrap_content"
             />
        <Button
            android:text="@string/button_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Reversed:"/>
</RelativeLayout>

And this is how it looks like:

问题

I want the TextView element to be below the EditText element in the LinearLayout, but the IDE doesn't allow me to do it since they're not in the same layout.

I want to keep the LinearLayout in order to keep the EditText's weight to allow it to stretch horizontally, though.

So how do I make the TextView in the RelativeLayout under the EditText in the LinearLayout? android:layout_below does not work since they are in different Layouts.

How do I solve it?

The answer is pretty simple. Add an id to the LinearLayout and then put the layout below of the TextView

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText
        android:text="Enter a message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:id="@+id/hi_field"
        android:layout_height="wrap_content"
         />
    <Button
        android:text="@string/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_below="@+id/linearLayout"
    android:text="Reversed:"/>

Here LinearLayout and TextView are direct children of RelativeLayout and so, android:layout_below will work in the same.

Just add this to your TextView

android:layout_below="@id/linearLayout"

You are using a RelativeLayout because of that the views are overlapping each other. Just add the code to the textView :D

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     
    android:layout_width="match_parent"
    android:layout_height="match_parent"      
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"      
    tools:context=".reverse_screen"
    android:textAllCaps="false">
    <LinearLayout
       android:id="@+id/top_linear"    
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal">
       <EditText
           android:text="Enter a message"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:id="@+id/hi_field"
           android:layout_height="wrap_content"
            />
       <Button
           android:text="@string/button_send"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
   </LinearLayout>
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/top_linear"
       android:textSize="20sp"
       android:text="Reversed:"/>
</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