[英]how to adjust the width of Android TextView dynamically
I have RelativeLayout in which I place a TextView to the left of a button which is alligned parent right.我有一个相对布局,其中我将一个 TextView 放置在一个与父右对齐的按钮的左侧。 So, when the text to be displayed in TextView increases more than wrap_Content then it overlaps on Button.
因此,当要在 TextView 中显示的文本增加超过 wrap_Content 时,它会在 Button 上重叠。 So, I need a solution to display the exceeded Text in next line.
所以,我需要一个解决方案来在下一行显示超出的文本。 Can anyone help me in sorting out this issue ?
谁能帮我解决这个问题?
In the below layout if the TextView2's Text is "11111111111111111111111" it will overlap with TextView1 .在下面的布局中,如果 TextView2 的 Text 是“11111111111111111111111”,它将与 TextView1 重叠。 How to prevent that ?
如何防止?
my layout is as below :我的布局如下:
<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>
In the above layout if the TextView2's Text is "11111111111111111111111" it will overlap with TextView1 .在上面的布局中,如果 TextView2 的 Text 是“11111111111111111111111”,它将与 TextView1 重叠。 How to prevent that
如何防止
Simply add android:layout_toLeftOf="@+id/button"
to your TextView to keep it from colliding with your Button.只需将
android:layout_toLeftOf="@+id/button"
到您的 TextView 以防止它与您的按钮发生冲突。
It will look like this:它看起来像这样:
<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>
You have changed the question... but I'll answer this new one too.你已经改变了问题……但我也会回答这个新问题。 You might be trying to squeeze too much into one row.
你可能试图挤进一排太多。 However let's change your layout to a horizontal LinearLayout and use a couple
layout_weight
attributes:但是,让我们将您的布局更改为水平 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>
As a note: textView2
does not wrap around to a new line because you used the ellipse
attribute.注意:
textView2
不会换行,因为您使用了ellipse
属性。 Good luck!祝你好运!
based on @Sam solution I added android:layoutDirection="rtl"
for desired result基于@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.