[英]Textview comes on top Imageview
我正在尝试在RelativeLayout
添加TextView
和ImageView
,如下所示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:background="@drawable/bg_message2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
android:textSize="16dp"
android:id="@+id/mUser"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mUserImg"
android:layout_alignParentRight="true"
android:background="@drawable/bg_profile_message"/>
</RelativeLayout>
如何防止在TextView
被上下面垫的ImageView
?
首先将ImageView设置到位,然后设置TextView。
您可以为TextView指定宽度“match_parent”,因为它将仅填充剩余空间。
所以:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="10dp"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mUserImg"
android:layout_alignParentRight="true"
android:background="@drawable/bg_profile_message"
/>
<TextView
android:background="@drawable/bg_message2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/mUserImg"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
android:textSize="16dp"
android:id="@+id/mUser"
/>
</RelativeLayout>
修改如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:background="@drawable/bg_message2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
android:textSize="16dp"
android:id="@+id/mUser"/>
<ImageView
android:layout_width="match_parent"
android:layout_weight="0.8"
android:layout_height="wrap_content"
android:id="@+id/mUserImg"
android:layout_alignParentRight="true"
android:background="@drawable/bg_profile_message"/>
</LinearLayout>
您可以根据需要减少和增加layout_weight的值
希望能帮助到你!!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:id="@+id/mUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/mUserImg"
android:background="@drawable/bg_message2"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
android:textSize="16dp" />
<ImageView
android:id="@+id/mUserImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/bg_profile_message" />
</RelativeLayout>
你可以使用它来为你的TextView:
layout_alignParentLeft="true"
这适用于ImageView的右侧:
android:layout_alignParentRight="true"
编辑
您可以为TextView
添加以下内容:
layout_toLeftOf="@+id/mUserImg"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.