简体   繁体   English

Android-在RelativeLayout中放置TextView

[英]Android - Positioning TextView in RelativeLayout

I'm making a small app for Android and I have a problem with the XAML. 我正在为Android开发一个小型应用,而XAML出现问题。

This is the XML layout code: 这是XML布局代码:

<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. 我希望TextView元素在LinearLayout中的EditText元素下方,但是IDE不允许我这样做,因为它们不在同一布局中。

I want to keep the LinearLayout in order to keep the EditText's weight to allow it to stretch horizontally, though. 我想保留LinearLayout以保持EditText的权重,以使其能够水平拉伸。

So how do I make the TextView in the RelativeLayout under the EditText in the LinearLayout? 那么,如何在LinearLayout中的EditText下的RelativeLayout中创建TextView? android:layout_below does not work since they are in different Layouts. android:layout_below无法使用,因为它们位于不同的布局中。

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 将一个ID添加到LinearLayout,然后将布局放在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. 这里LinearLayout和TextView是RelativeLayout的直接子代,因此android:layout_below可以在同一环境下工作。

Just add this to your TextView 只需将其添加到您的TextView

android:layout_below="@id/linearLayout"

You are using a RelativeLayout because of that the views are overlapping each other. 您正在使用RelativeLayout,因为视图彼此重叠。 Just add the code to the textView :D 只需将代码添加到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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