安卓LinearLayout - ImageView被推出屏幕

6
我使用下面发布的布局将消息显示为一行,通过将该布局作为一行插入到ListView中来实现。问题是,单行消息可以正常工作,但当有两行或更多行时,指示消息状态的ImageView将被推出屏幕,无法找到原因。有什么想法吗?下面是布局代码和截图。
这是已经充气的布局:
    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:padding="5dp" >

<TextView
    android:id="@+id/tvTimestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="8sp" />

<LinearLayout
    android:id="@+id/rl_message_holder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_toRightOf="@+id/tvTimestamp"
    android:background="@drawable/outgoing_bg"

    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvMessageOutgoing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imgMessageState"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="bottom"
        android:src="@drawable/ic_launcher" />

  </LinearLayout>

 </RelativeLayout>

如果您尝试删除android:layout_gravity="bottom"并将ImageView放置在TextView下方会怎样呢? - Milos Cuculovic
我需要将布局方向更改为垂直才能这样做。但是我想让图像位于textView的右下角,而不是在其下方。 - Droidman
好的,但是请尝试一次,看看问题是否源于此。 - Milos Cuculovic
5个回答

9

将您的线性布局宽度改为fill_parent,并在textview中添加android:layout_weight="1"


对我来说,解决办法只是权重。 - Uriel Frankel

4
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:id="@+id/tvTimestamp"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:gravity="center_vertical"
        android:text="10:20pm"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="8sp" />

    <RelativeLayout
        android:id="@+id/rl_message_holder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="80" >

        <TextView
            android:id="@+id/tvMessageOutgoing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/imgMessageState"
            android:padding="4dp"
            android:singleLine="false"
            android:text="android developer tutorial in the world"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imgMessageState"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="bottom"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

</LinearLayout>

2
这对我有用。
<TextView
        android:id="@+id/tvMessageOutgoing"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:layout_weight="1"
        android:text="Medium Tesssssssssssssssssssssssssssssseeeeeeeessssxt"
        android:textAppearance="?android:attr/textAppearanceMedium" />

这将使TextView占据剩余的空间。


1
尝试这样做。
   <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.6"
          >

          <ImageView
        android:id="@+id/imgMessageState"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="bottom"
        android:src="@drawable/ic_launcher" />

        </LinearLayout>

在线性布局中定义您的图像视图布局。

0
您可以使用


android:drawableRight="@drawable/ic_launcher"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接