如何动态更改TextView宽度

时间:2012-09-02 06:09:19

标签: android

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

注意:Textview tv.setWidth(pix)不起作用。当我试图找到tv.getWidth()的值时总是为零。

3 个答案:

答案 0 :(得分:2)

请尝试使用GONE而不是使用INVISIBLE。当使用INVISIBLE时,即使你看不到它,它仍然需要一个空间。

答案 1 :(得分:2)

`

    <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)

答案 2 :(得分:0)

将您的小部件包裹在RelativeLayout中,然后检查。如果隐身,您可以将textview置于button之上。如果您使用LinearLayout,则无法执行此操作,因为它会将小部件设置为水平或垂直且不重叠。

相关问题