【问题标题】:Positioning a view under two views (or the lowest view)将视图定位在两个视图(或最低视图)下
【发布时间】:2013-07-12 01:20:35
【问题描述】:

我有一个RelativeLayout,左边是ImageView,右边是TextViewTextView 是通过他们的 API 从网站下载的,所以它的内容每次都会不同。

我想在这两个下面有另一个TextView,但是当TextView 长度小于ImageView 时我遇到了问题。发生这种情况时,底部的TextView 将与ImageView 重叠,因为我将底部TextView 对齐到右上角的TextView 下方。

我需要做的是能够在最低的视图下对齐底部TextView

这是我的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <ImageView
        android:id="@+id/itemImageView"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/id_image" />

    <TextView
        android:id="@+id/itemContentsTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/itemImageView"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:text="Sample contents\nSample contents\nSample contents" />

    <TextView
        android:id="@+id/itemIdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/itemContentsTextView"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:text="1234" />

</RelativeLayout>

【问题讨论】:

    标签: android android-layout alignment


    【解决方案1】:

    您应该将LinearLayout 创建为顶级父级并将其设为orientation:vertical。然后先添加您的relativeLayout,然后添加您的TextView

    所以它看起来像这样。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical" >
        <RelativeLayout    
                   android:layout_width="match_parent"
                   android:layout_height="match_parent" >
    
            <ImageView
                    android:id="@+id/itemImageView"
                    android:layout_width="100dp"
                    android:layout_height="80dp"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/id_image" />
    
            <TextView
                    android:id="@+id/itemContentsTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/itemImageView"
                    android:layout_marginRight="2dp"
                    android:layout_marginTop="2dp"
                    android:text="Sample contents\nSample contents\nSample contents" />
        </RelativeLayout>
        <TextView
                android:id="@+id/itemIdTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:text="1234" />
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      将顶部 ImageViewTextView 包裹在另一个 RelativeLayout 中,并将其用作底部 TextView 的锚点。

      类似这样的东西(未测试):

      <?xml version="1.0" encoding="utf-8"?>
      <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" >
      
          <RelativeLayout
              android:id="@+id/wrappingLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" >
      
              <ImageView
                  android:id="@+id/itemImageView"
                  android:layout_width="100dp"
                  android:layout_height="80dp"
                  android:layout_alignParentLeft="true"
                  android:layout_marginLeft="5dp"
                  android:layout_marginRight="5dp"
                  android:layout_marginTop="5dp"
                  android:src="@drawable/id_image" />
      
              <TextView
                  android:id="@+id/itemContentsTextView"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignRight="@+id/itemImageView"
                  android:layout_marginRight="2dp"
                  android:layout_marginTop="2dp"
                  android:text="Sample contents\nSample contents\nSample contents" />
          </RelativeLayout>
      
          <TextView
              android:id="@+id/itemIdTextView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/wrappingLayout"
              android:layout_marginBottom="10dp"
              android:layout_marginRight="10dp"
              android:layout_marginTop="5dp"
              android:text="1234" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        再使用一个RelativeLayout 我已经测试过,文字从不重叠

        应该是这样的:

        <RelativeLayout
            android:id="@+id/temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        
            <ImageView
                android:id="@+id/itemImageView"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:src="@drawable/ic_launcher" />
        
            <TextView
                android:id="@+id/itemContentsTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/itemImageView"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:text="Sample contents\nSample contents\nSample contents" />
        </RelativeLayout>
        
        <TextView
            android:id="@+id/itemIdTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/temp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:text="1234" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 2012-03-24
          • 1970-01-01
          • 1970-01-01
          • 2019-11-24
          相关资源
          最近更新 更多