【问题标题】:Android - Positioning TextView in RelativeLayoutAndroid - 在RelativeLayout中定位TextView
【发布时间】:2015-11-02 07:31:48
【问题描述】:

我正在为 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 不起作用,因为它们位于不同的布局中。

我该如何解决?

【问题讨论】:

    标签: android android-layout layout textview


    【解决方案1】:

    答案很简单。 给LinearLayout添加一个id,然后把布局放到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 将在相同的情况下工作。

    【讨论】:

    • 哦,不知道你可以给 Layouts 一个 ID。
    • 非常感谢!如果可能的话,我会在 10 分钟内接受答案。
    • 说我7分钟后可以接受,到时候我就接受了。
    【解决方案2】:

    只需将其添加到您的 TextView

    android:layout_below="@id/linearLayout"
    

    您使用的是RelativeLayout,因为视图相互重叠。只需将代码添加到 textView :D

    【讨论】:

      【解决方案3】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-14
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多