你好
谁能告诉我们如何将xml布局更改为java代码。
我需要在选项卡 View 中显示网格 View 。为此,我需要在 Java 代码而不是 xml 中实现这些属性。请回复

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"

最佳答案

假设您访问了网格 View :

final GridView gw = [...];

要设置上述所有属性,您应该编写:
// This line applies to a GridView that you've just created
gv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// The next two lines are for a GridView that you already have displayed
gv.getLayoutParams().width = LayoutParams.FILL_PARENT;
gv.getLayoutParams().height = LayoutParams.FILL_PARENT;

gv.setNumColumns(GridView.AUTO_FIT);
gv.setVerticalSpacing(convertFromDp(10));
gv.setHorizontalSpacing(convertFromDp(10));
gv.setColumnWidth(convertFromDp(90));
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setGravity(Gravity.CENTER);

要设置 LayoutParams ,您必须在上述两种情况之间进行选择。

10-07 19:19
查看更多