簡體   English   中英

Android-在RelativeLayout中放置TextView

[英]Android - Positioning TextView in RelativeLayout

我正在為Android開發一個小型應用,而XAML出現問題。

這是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>

這是這樣的:

問題

我希望TextView元素在LinearLayout中的EditText元素下方,但是IDE不允許我這樣做,因為它們不在同一布局中。

我想保留LinearLayout以保持EditText的權重,以使其能夠水平拉伸。

那么,如何在LinearLayout中的EditText下的RelativeLayout中創建TextView? android:layout_below無法使用,因為它們位於不同的布局中。

我該如何解決?

答案很簡單。 將一個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:"/>

這里LinearLayout和TextView是RelativeLayout的直接子代,因此android:layout_below可以在同一環境下工作。

只需將其添加到您的TextView

android:layout_below="@id/linearLayout"

您正在使用RelativeLayout,因為視圖彼此重疊。 只需將代碼添加到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