我已经开始工作 android 应用程序,我设计的布局RelativeLayout
是 的父级GridView
,我只是想以LayoutParams
编程方式设置GridView
。
下面是我的布局和代码,我在做什么:
布局.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="510dp"
android:gravity="center"
android:horizontalSpacing="@dimen/image_grid_hspacing"
android:numColumns="@integer/grid_num_cols"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/image_grid_vspacing"
android:background="#FFFFFF" />
</RelativeLayout>
</ScrollView>
代码 :
GridView gridview = (GridView) rootView.findViewById (R.id.gridview);
gridview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 300));
当我启动我的应用程序时,它总是崩溃并且错误 logcat 说
错误 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
对不起一个愚蠢的问题,我不知道它抛出异常的原因。请分享您对例外情况的可能性的问题的看法