Issues adding child to GridLayout dynamically in android

╄→尐↘猪︶ㄣ 提交于 2019-12-04 09:57:00

Take a look at where you are adding the linear layout to the grid view in onCreate(). Early on you are setting the layout params as follows:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(0, 0));

Yet, later on, you are adding the linear layouts with a different set of layout params as follows:

GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
myGLP.rowSpec = rowSpec;
myGLP.columnSpec = colSpec;
layout.addView(linearLayout, myGLP);

This will negate the earlier layout parameters. I suggest you change the code to the following:

GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
myGLP.rowSpec = rowSpec;
myGLP.columnSpec = colSpec;
myGLP.width = 0;
myGLP.height = 0;
layout.addView(linearLayout, myGLP);

You can delete the earlier code.

Here is a video of the result:

I recommend you, use google flexbox-layout instead of support GridLayout. its much more flexible and also stable and easy to use.

See more about it in github.

UPDATE:

This is sample for static XML layout:

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

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:paddingBottom="70dp"
        android:paddingEnd="20dp"
        android:paddingStart="20dp"
        android:paddingTop="70dp"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexDirection="row"
        app:flexWrap="wrap">

        <LinearLayout
            android:id="@+id/row0col0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row0col1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FFFF00"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row0col2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF00FF"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:background="#0000FF"
            android:orientation="horizontal"
            app:layout_flexGrow="1"
            app:layout_wrapBefore="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#00FFFF"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            android:orientation="horizontal"
            app:layout_flexGrow="1"
            app:layout_wrapBefore="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FFFF00"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF00FF"
            android:orientation="horizontal"
            app:layout_flexGrow="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

        </LinearLayout>

    </com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>

If you want dynamically add children, see this sample:

XML layout:

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

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:paddingBottom="70dp"
        android:paddingEnd="20dp"
        android:paddingStart="20dp"
        android:paddingTop="70dp"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexDirection="row"
        app:flexWrap="wrap"/>

</RelativeLayout>

Code:

public class PuzzleActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.puzzle_dynamic);

        FlexboxLayout layout = (FlexboxLayout) findViewById(R.id.flexLayout);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                LinearLayout linearLayout = new LinearLayout(this);
                linearLayout.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                //linearLayout.setId(R.id.row + i + R.id.col + j);
                linearLayout.setGravity(Gravity.FILL_HORIZONTAL);
                linearLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.layout_background));
                ImageView imageView = new ImageView(this);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                imageView.setAdjustViewBounds(true);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                linearLayout.addView(imageView);

                FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT);
                lp.setFlexGrow(1);

                if (j == 0)
                    lp.setWrapBefore(true);//notice to this!

                layout.addView(linearLayout, lp);
            }
        }
    }
}

For add flexbox to code project, put this line in your gradle dependency:

compile 'com.google.android:flexbox:0.3.0'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!