无法以编程方式使用Gridlayout创建视图

时间:2014-02-13 15:32:08

标签: android android-layout android-linearlayout android-gridlayout

我在android中创建了一个类似于以下内容的图片库:image gallery

我使用Gridlayout作为主要布局,使用Linearlayouts作为项目。我就是这样做的:

//this is a method that returns a polaroid tile that I add in gridlayout
public LinearLayout ConstructAlbumGridLinearLayout(final int id) {

        LinearLayout albumLayout = new LinearLayout(
                context.getApplicationContext());
        RelativeLayout albumDetailsLayout = new RelativeLayout(
                context.getApplicationContext());
        TextView albumName = new TextView(context.getApplicationContext());
        TextView albumPicturesNumber = new TextView(
                context.getApplicationContext());
        LinearLayout pictureHolder = new LinearLayout(
                context.getApplicationContext());

        LinearLayout pictureRowOneHolder = new LinearLayout(
                context.getApplicationContext());
        LinearLayout pictureRowTwoHolder = new LinearLayout(
                context.getApplicationContext());

//this is the line, which throws an exception, even if I do wrap_content
//      albumLayout.setLayoutParams(new LayoutParams(100,
//              100));

        pictureRowOneHolder.setOrientation(LinearLayout.HORIZONTAL);
        pictureRowOneHolder.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        pictureRowTwoHolder.setOrientation(LinearLayout.HORIZONTAL);
        pictureRowTwoHolder.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        pictureHolder.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        pictureHolder.setOrientation(LinearLayout.VERTICAL);

        albumLayout.setOrientation(LinearLayout.VERTICAL);

        albumDetailsLayout.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        RelativeLayout.LayoutParams rlName = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rlName.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        albumName.setLayoutParams(rlName);
        albumName.setText("Family");

        RelativeLayout.LayoutParams rlSize = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rlSize.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        albumPicturesNumber.setLayoutParams(rlSize);
        albumPicturesNumber.setText("3");

        for (int j = 0; j < 2; ++j) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    30, 30);
            ImageView albumPic = new ImageView(context.getApplicationContext());
            albumPic.setImageResource(R.drawable.movie1);
            albumPic.setLayoutParams(layoutParams);
            pictureRowOneHolder.addView(albumPic);
        }

        for (int j = 0; j < 2; ++j) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    30, 30);
            ImageView albumPic = new ImageView(context.getApplicationContext());
            albumPic.setImageResource(R.drawable.movie1);
            albumPic.setLayoutParams(layoutParams);
            pictureRowTwoHolder.addView(albumPic);
        }

        pictureHolder.addView(pictureRowOneHolder);
        pictureHolder.addView(pictureRowTwoHolder);

        albumDetailsLayout.addView(albumName);
        albumDetailsLayout.addView(albumPicturesNumber);

        albumLayout.addView(albumDetailsLayout);
        albumLayout.addView(pictureHolder);

        return albumLayout;

    }

然后我在网格布局中添加此布局:

gl.addView(pictureGallery.ConstructAlbumGridLinearLayout(i));

这是我的XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/albumLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/pictureType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Albums"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <GridLayout 
        android:id="@+id/albumMainGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我不确定我在这里做错了什么......它是网格布局本身吗?或者带有gridlayout的Linearlayout被证明是个问题?

编辑 - 以下是例外:

02-13 20:11:28.250: E/AndroidRuntime(5040): FATAL EXCEPTION: main
02-13 20:11:28.250: E/AndroidRuntime(5040): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.GridLayout$LayoutParams
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.GridLayout.getLayoutParams1(GridLayout.java:729)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.GridLayout.validateLayoutParams(GridLayout.java:663)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.GridLayout.getLayoutParams(GridLayout.java:734)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:866)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.GridLayout.onMeasure(GridLayout.java:894)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.View.measure(View.java:12775)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.View.measure(View.java:12775)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.View.measure(View.java:12775)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.View.measure(View.java:12775)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2240)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.View.measure(View.java:12775)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1151)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2539)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.os.Looper.loop(Looper.java:137)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at android.app.ActivityThread.main(ActivityThread.java:4514)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-13 20:11:28.250: E/AndroidRuntime(5040):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

从您的Logcat看起来您正在向ViewGroup.LayoutParams投射GridLayout.LayoutParams类。当您调用LayoutParams的构造函数方法时,这是一个常见问题:

albumLayout.setLayoutParams(new LayoutParams(100, 100));

您希望具体使用LayoutParams,因此如果您使用GridLayout,请使用GridLayout.LayoutParamsLinearLayout使用LinearLayout.LayoutParams等。停止拾取错误的版本,因为您总是希望使用 ViewGroup的父类。

在上面的示例中,albumLayoutGridLayout,因此您要使用GridLayout.LayoutParams。将该行更改为:

albumLayout.setLayoutParams(new GridLayout.LayoutParams(100, 100));

您还有几个实例,其中LinearLayoutsRelativeLayout可以使用相同的更改。 (LinearLayout.LayoutParamsRelativeLayout.LayoutParams)。