简体   繁体   English

如何动态更改TextView宽度

[英]How to change a TextView width Dynamically

I have a TextView1, buttons1 and buttons2 Horizontally. 我有一个水平的TextView1,buttons1和button2。 When Button2 click Button1 is invisible. 当Button2单击时,Button1不可见。 When Button1 is invisible, I need to increase the width of the textview to cover the space created when button1 is invisible and vice versa. 当Button1不可见时,我需要增加textview的宽度以覆盖button1不可见时创建的空间,反之亦然。 Can Please anybody help How can I change the Textview width dynamically. 可以请任何人帮助如何动态地更改Textview的宽度。

NB: Textview tv.setWidth(pix) is not working. 注意:Textview tv.setWidth(pix)不起作用。 when I tried to find the value of tv.getWidth() gives always zero. 当我尝试查找tv.getWidth()的值时,始终为零。

Please try to use GONE instead of using INVISIBLE. 请尝试使用GONE而不是INVISIBLE。 When using INVISIBLE even though you can't see it, it still takes a space. 在使用INVISIBLE时,即使您看不到它,也要占用一个空间。

` `

    <TextView
        android:layout_width="odp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="odp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello"
        android:visibility="visible" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="odp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello" />
</LinearLayout>`you can use weight for three element and button1.setVisibility(View.GONE)

Wrap your widgets in a RelativeLayout and then check. 将小部件包装在RelativeLayout ,然后检查。 You will be able to bring your textview on top of your button if it is invisble. 你将能够把你textview在你的顶部button如果是invisble。 You cant do this if you are using LinearLayout because it sets widgets horizontaly or verticaly and does not overlap. 如果您使用LinearLayout ,则无法执行此操作,因为它会将小部件设置为水平或垂直,并且不会重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM