我想在java中创建一个gridview,我不想使用xml。下面的示例是XML作为布局。它显示了3列。我只想用java来实现这个设置。
在我的活动中,而不是GridView gv = (GridView) findViewById (R.id.gridview);
我使用GridView gv = new GridView (this);
现在我唯一的问题是它只有一个列。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

</LinearLayout>

最佳答案

相当于android:numColumns的代码是setNumColumns(int)。对于id之类的东西,您需要返回查看文档。宽度和高度将用layoutparams设置。
GridView Documentation在android developer.com上

07-26 09:39
查看更多