使ImageView保持正确的文本大小,但又防止长文本将其推出视野?

san*_*one 3 android android-layout

无论我使用LinearLayout还是,情况都是一样的RelativeLayout。我认为这是一个旧的Android XMl错误,但我不知道为什么它仍然存在。

即,当文本太长时,在a ImageView右侧的布局TextView可能会从屏幕上消失。它只是将其推离屏幕。我不能TextView单行。

这是XML。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
>

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="some long text blahblahblahblahblah"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="17sp"
    android:layout_marginRight="10dp"
    android:layout_centerVertical="true"
    />

<ImageView
    android:layout_toRightOf="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/key"
    android:layout_centerVertical="true"
    />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我无法使用layout_weight属性,因为图像将粘贴在屏幕的右侧。它必须在文本的右侧。

任何人有任何想法如何解决此错误?

文字长短时检查图像。在第二张图像上,文本很长,图像被推离屏幕。

Eri*_* B. 6

如果使用layout_weight正确,则可以完成任务。请参考以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="The quick brown fox jumps over the lazy dog. As you can see it easily handles the long length of the text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="17sp"
        android:layout_marginRight="10dp"

        />

    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

长文本的输出:输出长文本

短文本的输出: 在此处输入图片说明

编辑: 之所以有效,是因为其宽度设置为的布局wrap_content。如果match_parent在所有情况下都是这样,那么所有的额外空间都将被分配给TextView它,layout_weight但是由于父级为wrap_content,所以TextView当文本较小时,将没有多余的空间可用于填充。但是,当文本较大时,将weight属性应用于,TextView但是由于没有属性layout_weightImageView因此仅占用其必须的空间量。