GridLayout (not GridView) how to stretch all children evenly

后端 未结 20 1894
独厮守ぢ
独厮守ぢ 2020-11-22 09:35

I want to have a 2x2 grid with a buttons inside. This is only ICS so I am trying to use the new GridLayout given.

Here\'s the XML of my layout:

 <         


        
相关标签:
20条回答
  • 2020-11-22 10:18

    I finally found the solution. As Rotemmiz said, you have to do it dynamically afterwards. This code stretches the buttons to fill the view horizontally, but the same can be done for vertically.

    public void fillview(android.support.v7.widget.GridLayout gl)
    {
        Button buttontemp;
    
        //Stretch buttons
        int idealChildWidth = (int) ((gl.getWidth()-20*gl.getColumnCount())/gl.getColumnCount());
        for( int i=0; i< gl.getChildCount();i++)
        {
            buttontemp = (Button) gl.getChildAt(i);
            buttontemp.setWidth(idealChildWidth);
        }
    }
    

    (The 20 is for the internal and external padding and margins. This could be done more universally, but this is far cleaner)

    Then it can be called like this:

        android.support.v7.widget.GridLayout gl = (android.support.v7.widget.GridLayout)findViewById(R.id.buttongrid);
        ViewTreeObserver vto = gl.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Override public void onGlobalLayout() 
        {
    
                android.support.v7.widget.GridLayout gl = (android.support.v7.widget.GridLayout) findViewById(R.id.buttongrid);
                fillview(gl);
    
                ViewTreeObserver obs = gl.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
        }});
    

    It must be done with an observer because we need to wait for the view to be drawn before we call the views.

    0 讨论(0)
  • 2020-11-22 10:19

    Result :

    Try something like this :

        final int MAX_COLUMN = gridView.getColumnCount(); //5
        final int MAX_ROW = gridView.getRowCount(); //7
        final int itemsCount = MAX_ROW * MAX_COLUMN; //35
    
        int row = 0, column = 0;
    
        for (int i = 0; i < itemsCount; i++) {
            ImageView view = new ImageView(this);
    
            //Just to provide alternate colors
            if (i % 2 == 0) {
                view.setBackgroundColor(Color.RED);
            } else {
                view.setBackgroundColor(Color.GREEN);
            }
    
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(row, 1F), GridLayout.spec(column, 1F));
            view.setLayoutParams(params);
            gridView.addView(view);
    
            column++;
    
            if (column >= MAX_COLUMN) {
                column = 0;
                row++;
            }
        }
    

    If you want specific width and height for your cells, then use :

         params.width = 100; // Your width
         params.height = 100; //your height
    
    0 讨论(0)
  • 2020-11-22 10:20

    i have same problem and i think just set width and height programmatically :

    set width of button's with metrics.widthPixels / 2 metrics is object of DisplayMetrics.

     GridLayout gridLayout=findViewById(R.id.grid);
        for (int i = 0; i <gridLayout.getChildCount() ; i++) {
            View view= gridLayout.getChildAt(i);
            if(view instanceof Button){
                Button btn = (Button) view;
                btn.setWidth(200);//metrics.widthPixels / 2
                btn.setHeight(200);//metrics.heightPixels / 7
            }
        } 
    
    0 讨论(0)
  • 2020-11-22 10:22

    Try adding the following to your GridLayout spec. That should work.

    android:useDefaultMargins="true"
    
    0 讨论(0)
  • 2020-11-22 10:22

           <GridLayout
            android:layout_width="match_parent"
            android:layout_weight="3"
            android:columnCount="2"
            android:padding="10dp"
            android:rowCount="3"
            android:background="@drawable/background_down"
            android:layout_height="0dp">
    
    
            <androidx.cardview.widget.CardView
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_margin="10dp"
                android:elevation="10dp"
                app:cardCornerRadius="15dp"
                >
                <LinearLayout
                    android:weightSum="3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >
    
                    <ImageView
                        android:layout_weight="2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_margin="15dp"
                        android:src="@drawable/user" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Users"
                        android:textSize="16sp"
                        android:layout_marginStart="15dp"
                        android:layout_marginLeft="15dp" />
                </LinearLayout>
    
            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_margin="10dp"
                android:elevation="10dp"
                app:cardCornerRadius="15dp"
                >
                <LinearLayout
                    android:weightSum="3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >
    
                    <ImageView
                        android:layout_weight="2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_margin="15dp"
                        android:src="@drawable/addusers" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Add Users"
                        android:textSize="16sp"
                        android:layout_marginStart="15dp"
                        android:layout_marginLeft="15dp" />
                </LinearLayout>
    
    
            </androidx.cardview.widget.CardView>
    
            <androidx.cardview.widget.CardView
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_margin="10dp"
                android:elevation="10dp"
                app:cardCornerRadius="15dp"
                >
                <LinearLayout
                    android:weightSum="3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >
    
                    <ImageView
                        android:layout_weight="2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_margin="15dp"
                        android:src="@drawable/newspaper" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Report"
                        android:textSize="16sp"
                        android:layout_marginStart="15dp"
                        android:layout_marginLeft="15dp" />
                </LinearLayout>
    
    
    
            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_margin="10dp"
                android:elevation="10dp"
                app:cardCornerRadius="5dp"
                >
                <LinearLayout
                    android:weightSum="3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >
    
                    <ImageView
                        android:layout_weight="2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_margin="15dp"
                        android:src="@drawable/settings" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Settings"
                        android:textSize="16sp"
                        android:layout_marginStart="15dp"
                        android:layout_marginLeft="15dp" />
                </LinearLayout>
    
    
            </androidx.cardview.widget.CardView>
    
        </GridLayout>
    

    you can find the whole tutorials here, Android Grid Layout With CardView and OnItemClickListener

    0 讨论(0)
  • 2020-11-22 10:23

    This is the right answer

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/favorites_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:rowCount="2"
        android:columnCount="2">
        <Button
            android:text="Cell 0"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:textSize="14dip" 
            />
        <Button
            android:text="Cell 1"
            android:layout_row="0"
            android:layout_column="1"
            android:textSize="14dip"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
    
        <Button
            android:text="Cell 2"
            android:layout_row="1"
            android:layout_column="0"
            android:textSize="14dip"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:text="Cell 3"
            android:layout_row="1"
            android:layout_column="1"
            android:textSize="14dip"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
    </GridLayout>
    
    0 讨论(0)
提交回复
热议问题