繁体   English   中英

如何动态更改TextView宽度

[英]How to change a TextView width Dynamically

我有一个水平的TextView1,buttons1和button2。 当Button2单击时,Button1不可见。 当Button1不可见时,我需要增加textview的宽度以覆盖button1不可见时创建的空间,反之亦然。 可以请任何人帮助如何动态地更改Textview的宽度。

注意:Textview tv.setWidth(pix)不起作用。 当我尝试查找tv.getWidth()的值时,始终为零。

请尝试使用GONE而不是INVISIBLE。 在使用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)

将小部件包装在RelativeLayout ,然后检查。 你将能够把你textview在你的顶部button如果是invisble。 如果您使用LinearLayout ,则无法执行此操作,因为它会将小部件设置为水平或垂直,并且不会重叠。

暂无
暂无

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

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