简体   繁体   English

Textview位于顶部Imageview

[英]Textview comes on top Imageview

I'm trying to add a TextView and an ImageView in a RelativeLayout , as follows 我正在尝试在RelativeLayout添加TextViewImageView ,如下所示

<?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>

The above code looks like this: 上面的代码如下所示: http://i.stack.imgur.com/5vhsc.png

and What I want : 我想要的是什么: http://i.stack.imgur.com/PVDIK.jpg

how do I prevent the TextView being underlaid on the ImageView ? 如何防止在TextView被上下面垫的ImageView

First set the ImageView in place, then set the TextView. 首先将ImageView设置到位,然后设置TextView。
You can give the TextView a width of "match_parent", because it will fill just the remaining space. 您可以为TextView指定宽度“match_parent”,因为它将仅填充剩余空间。

So: 所以:

<?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>

Modify like this: 修改如下:

<?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>

You can reduce and increase the value of layout_weight depending on your needs 您可以根据需要减少和增加layout_weight的值

Hope it helps!!! 希望能帮助到你!!!

<?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>

You can with this for your TextView : 你可以使用它来为你的TextView:

layout_alignParentLeft="true" 

and this for right side for your ImageView : 这适用于ImageView的右侧:

android:layout_alignParentRight="true"

EDIT 编辑

And you can add this for your TextView : 您可以为TextView添加以下内容:

layout_toLeftOf="@+id/mUserImg"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM