【问题标题】:TextView goes behind the button in RelativeLayoutTextView 位于 RelativeLayout 中的按钮后面
【发布时间】:2017-12-14 13:52:50
【问题描述】:

我有一个 RelativeLayout,其中有一个 TextView 和一个按钮。按钮有一个属性android:layout_alignParentBottom="true",没关系。问题是 TextView:如果文本很大,它会在按钮后面。

RelativeLayout

<RelativeLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/absoluteLayout1">
    <TextView
        android:text="Long long string"
        android:textSize="16dp"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#d9e700"
        android:id="@+id/text"
        android:layout_marginBottom="12.5dp" />
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/button1" />
</RelativeLayout>

我需要的是不会出现在按钮后面的可调整大小的 TextView。如果文本很大,我们只能看到适合顶部和按钮之间大小的文本部分。 类似于两行,其中第 2 行(带按钮)具有固定高度,第 1 行可调整大小。 有人知道怎么做吗?

【问题讨论】:

    标签: android xml android-layout xamarin


    【解决方案1】:

    添加:

    android:layout_above="@+id/button1"
    

    到你的 TextView 像:

    <RelativeLayout android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/absoluteLayout1">
        <TextView
            android:text="Long long string"
            android:textSize="16dp"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#d9e700"
            android:id="@+id/text"
            android:layout_above="@+id/button1"
            android:layout_marginBottom="12.5dp" />
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:id="@+id/button1" />
    </RelativeLayout>
    

    【讨论】:

    • 就一行,OMG...非常感谢,真的解决了我的问题!
    • 在 api 18 中工作。没有 layout_above 选项。
    【解决方案2】:

    您需要对齐按钮上方的文本视图。使用 layout_above 属性。 下面是一个例子。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/absoluteLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="25px"
    android:minWidth="25px">
    
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="12.5dp"
        android:text="Long long string"
        android:textColor="#d9e700"
        android:textSize="16dp" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />
    

    【讨论】:

      【解决方案3】:

      1) 在您的文本视图中添加 android:layout_above="@id/button1"
      2) 将您的 textview 放在您的按钮下方,以便您可以为您的 textview 提供按钮的引用。

      <RelativeLayout
          android:id="@+id/absoluteLayout1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:minHeight="25px"
          android:minWidth="25px">
      
          <Button
              android:id="@+id/button1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:text="Button" />
      
          <TextView
              android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@id/button1"
              android:layout_alignParentTop="true"
              android:layout_marginBottom="12.5dp"
              android:text="Long long string"
              android:textColor="#d9e700"
              android:textSize="16dp" />
      </RelativeLayout>
      

      【讨论】:

      • 感谢您的回复!
      猜你喜欢
      • 2015-11-02
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2012-10-17
      • 2015-01-14
      • 2013-06-21
      相关资源
      最近更新 更多