左边有RelativeLayout
ImageView
,右边有TextView
。 TextView
是通过其网站从网站下载的,因此每次内容都会有所不同。
我希望在这两个下面有另一个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>
答案 0 :(得分:4)
您应该将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>
答案 1 :(得分:3)
将顶部ImageView
和TextView
换成另一个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>
答案 2 :(得分:1)
再使用一个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" />