【问题标题】:how to adjust the width of Android TextView dynamically如何动态调整Android TextView的宽度
【发布时间】:2012-09-06 19:29:50
【问题描述】:

我有RelativeLayout,在其中我将TextView 放置在与父右对齐的按钮的左侧。因此,当要在 TextView 中显示的文本增加超过 wrap_Content 时,它会在 Button 上重叠。所以,我需要一个解决方案来在下一行显示超出的文本。 谁能帮我解决这个问题?
在下面的布局中,如果 TextView2 的文本是“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 android-layout


    【解决方案1】:

    只需将 android:layout_toLeftOf="@+id/button" 添加到您的 TextView 以防止它与您的 Button 发生冲突。

    看起来像这样:

    <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 属性。祝你好运!

    【讨论】:

    • 在上述布局中,如果 TextView2 的文本为“11111111111111111111111”,它将与 TextView1 重叠。如何预防?
    • 同理。添加layout_toLeftOflayout_toRightOf 以防止一个TextView 写在另一个之上。
    • 在当前 TextView 之前再添加一个 TextView 并将当前 textview 添加到它的右侧。然后,如果我添加“111111111111111111111111”,那么它会在第一个 TextView 上重叠
    【解决方案2】:

    基于@Sam 解决方案,我添加了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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2015-01-27
      • 2021-01-21
      • 2011-04-13
      • 1970-01-01
      • 2018-10-30
      • 2015-04-14
      相关资源
      最近更新 更多