我已经在我的xml文件中构建了一个GridView,现在我想在Fragment类中填充它。 (此Fragment类使用该布局)
userhome.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/user_avatar"
android:paddingTop="16dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
public class UserHomeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.userhome, container, false);
}
我真的不知道如何采取我的“home_gridview”,我尝试了一切,但没有奏效! 我需要这个,因为我想在我的一个操作栏选项卡中使用它。你能帮我一些代码吗? 谢谢你们!
答案 0 :(得分:2)
将您的GridView
包裹在LinearLayout
(或其他一些布局)中,如下所示:
<LinearLayout
GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView android:id="@+id/home_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/user_avatar"
android:paddingTop="16dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
现在在onCreateView
找到您的GridView
ID:
public class UserHomeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.userhome, container, false);
GridView gridView = (GridView) view.findViewById(R.id.home_gridview);//must be your R not android.R
//use your grid view
return view;
}