android java代码中设置控件的宽高单位是什么?

比如:textView.setWidth(20);里面的20单位是什么?dp还是px?
2025-03-24 15:45:43
推荐回答(3个)
回答1:

android中的控件如果在xml布局文件中把控件的layout_width和layout_height写成固定值了,好像就不能再在程序中更改该控件的高度和宽度了,不知哪位大侠有何良策可以指教一二,如

xml文件内容如下:

android:id="@id/dialog_bottom_neutral"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:gravity="center" >

android:id="@id/dialog_bottom_neutral_button"

android:layout_width="80.0dip"

android:layout_height="28.0dip"

android:background="@drawable/dlg_button"

android:gravity="center"

android:singleLine="true"

android:text="mid"

android:textColor="@drawable/dlg_button_text_color"

android:textSize="14.0sp" />

     总结:如何在程序中动态设置button的宽度和高度,在程序中使用button.width和button.height设置没用,用LayoutParmas设置也没用。






回答2:

代码中设置的单位是px

回答3:

代码中默认的单位是px,即20px