将imageview放在textview旁边 - (图像视图重复)CHAT UI

时间:2017-11-17 13:29:27

标签: android xml android-layout android-studio user-interface

我想将imageview放在textview旁边。

这是我的XML:



<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    >
    <RelativeLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <TextView
        android:id="@+id/leftText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_margin="10dp"
        android:background="@drawable/rounded_corner"
        android:elevation="2dp"
        android:fontFamily="@font/maven_pro_black"
        android:padding="16dp"
        android:layout_marginTop="10dp"
        android:layout_weight="6"
        android:text="Hello this is me!!"
        android:textSize="14sp"
        android:textColor="#212121"
        />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/leftText"
            android:layout_weight="3"
            android:src="@drawable/user_2"
            android:layout_toRightOf="@+id/leftText"/>

     </RelativeLayout>

   <RelativeLayout
        android:layout_below="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/relativeLayout">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/rightText"
            android:layout_weight="3"
            android:src="@drawable/user_1" />

        <TextView
            android:id="@+id/rightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_margin="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="6"
            android:background="@drawable/rounded_corner1"
            android:elevation="2dp"
            android:fontFamily="@font/maven_pro_black"
            android:padding="16dp"
            android:text="Hi!! How are you!!"
            android:textColor="#fff"
            android:textSize="16sp" />

    </RelativeLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

这就是它在现实世界中的样子:

In the picture below you can see the repeated images

P.S。我试过&amp;而且没有工作:

机器人:layout_alignParentRight =&#34;真&#34;

机器人:layout_toLeftOf =&#34; @ + ID / BLABLA&#34; 甚至

机器人:drawableLeft =&#34; @可绘制/ your_image_name&#34;

这是好尝试,但不是我想要的。如何在SCREENSHOT中看到这种可重复的图像?感谢。

EDIT补充了我的按摩活动:

&#13;
&#13;
adapter = new FirebaseRecyclerAdapter<ChatMessage, chat_rec>(ChatMessage.class,R.layout.msglist,chat_rec.class,ref.child("chat")) {
            @Override
            protected void populateViewHolder(chat_rec viewHolder, ChatMessage model, int position) {

                if (model.getMsgUser().equals("user")) {

                    viewHolder.rightText.setText(model.getMsgText());
                    
                    viewHolder.rightText.setVisibility(View.VISIBLE);
                    viewHolder.leftText.setVisibility(View.GONE);

                }
                else {
                    viewHolder.leftText.setText(model.getMsgText());

                    viewHolder.rightText.setVisibility(View.GONE);
                    viewHolder.leftText.setVisibility(View.VISIBLE);

                }
            }
        };
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您需要将RelativeLayout更改为LinearLayout,如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <TextView
        android:id="@+id/leftText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_margin="10dp"
        android:background="@drawable/rounded_corner"
        android:elevation="2dp"
        android:fontFamily="@font/maven_pro_black"
        android:padding="16dp"
        android:layout_marginTop="10dp"
        android:layout_weight="6"
        android:text="Hello this is me!!"
        android:textSize="14sp"
        android:textColor="#212121"
        />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/leftText"
            android:layout_weight="3"
            android:src="@drawable/user_2"
            android:layout_toRightOf="@+id/leftText"/>

     </LinearLayout>

   <LinearLayout
        android:layout_below="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/relativeLayout">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/rightText"
            android:layout_weight="3"
            android:src="@drawable/user_1" />

        <TextView
            android:id="@+id/rightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_margin="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="6"
            android:background="@drawable/rounded_corner1"
            android:elevation="2dp"
            android:fontFamily="@font/maven_pro_black"
            android:padding="16dp"
            android:text="Hi!! How are you!!"
            android:textColor="#fff"
            android:textSize="16sp" />

    </LinearLayout>
</RelativeLayout>

答案 1 :(得分:0)

以下是我的回答:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    >
    <RelativeLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        >
    <TextView
        android:id="@+id/leftText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/rounded_corner"
        android:elevation="2dp"
        android:fontFamily="@font/maven_pro_black"
        android:padding="16dp"
        android:layout_marginTop="10dp"
        android:layout_weight="6"
        android:text="Hello this is me!!"
        android:textSize="14sp"
        android:textColor="#212121"
        />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/leftText"
            android:layout_weight="3"
            android:src="@drawable/user_2"
            android:layout_toRightOf="@+id/leftText"/>

     </RelativeLayout>

   <RelativeLayout
        android:layout_below="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:id="@+id/relativeLayout">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_weight="3"
            android:src="@drawable/user_1" />

        <TextView
            android:id="@+id/rightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="6"
            android:background="@drawable/rounded_corner1"
            android:layout_toEndOf="@+id/imageView3"
            android:elevation="2dp"
            android:fontFamily="@font/maven_pro_black"
            android:padding="16dp"
            android:text="Hi!! How are you!!"
            android:textColor="#fff"
            android:textSize="16sp" />

    </RelativeLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

RelativeLayout没有属性方向。我刚刚将您的属性alignParent从TextView移动到RelativeLayout(TextView的父级)

答案 2 :(得分:0)

好的,我做了解决方法,它正在以某种方式工作。 我将此添加到我的布局中 - &gt; textview - &gt;

        android:drawableLeft="@drawable/user_1"
        android:drawablePadding="6dp"
        android:paddingLeft="30dip"
        android:paddingRight="26dip"
        android:singleLine="true"
        android:gravity="center"

android:drawablePadding =“6dp”和android:drawableLeft =“@ drawable / user_1”使解决方案在某种程度上可以接受。我希望这对某人有用。亲切的问候。