[英]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.