[英]how to adjust the width of Android TextView dynamically
我有一个相对布局,其中我将一个 TextView 放置在一个与父右对齐的按钮的左侧。 因此,当要在 TextView 中显示的文本增加超过 wrap_Content 时,它会在 Button 上重叠。 所以,我需要一个解决方案来在下一行显示超出的文本。 谁能帮我解决这个问题?
在下面的布局中,如果 TextView2 的 Text 是“11111111111111111111111”,它将与 TextView1 重叠。 如何防止?
我的布局如下:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="@drawable/icon" >
</ImageView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_margin="5dp"
android:text="TextView"
android:textSize="22dp"
android:layout_toRightOf="@id/imageView1"
>
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="Button"
android:layout_alignTop="@+id/imageView1"
>
</Button>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button1"
android:text="TextView222"
android:textSize="22dp"
android:layout_margin="5dp"
android:layout_alignTop="@+id/imageView1"
android:ellipsize="end"
>
</TextView>
在上面的布局中,如果 TextView2 的 Text 是“11111111111111111111111”,它将与 TextView1 重叠。 如何防止
只需将android:layout_toLeftOf="@+id/button"
到您的 TextView 以防止它与您的按钮发生冲突。
它看起来像这样:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button"
android:text="11111111111111111111111111111111111111111111111111111111111111111111111111111111111"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Potatoe"/>
</RelativeLayout>
你已经改变了问题……但我也会回答这个新问题。 你可能试图挤进一排太多。 但是,让我们将您的布局更改为水平 LinearLayout 并使用几个layout_weight
属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="111111111111111111111111111111111"
android:textSize="22dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:ellipsize="end"
android:text="222222222222222222222222222222222"
android:textSize="22dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="Button"/>
</LinearLayout>
注意: textView2
不会换行,因为您使用了ellipse
属性。 祝你好运!
基于@Sam 解决方案,我添加了android:layoutDirection="rtl"
layoutDirection android:layoutDirection="rtl"
以获得所需的结果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button"
android:text="1111111111111111111111111111111111111"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Potatoe"/>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.