GridLayout (not GridView) how to stretch all children evenly

后端 未结 20 1918
独厮守ぢ
独厮守ぢ 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:11

    The best solution I could find is to use a linear layout(horizontal) for each row you want and within it assign the button (cell) width to 0dp and the weight to 1. For each of the linear layouts(rows) assign the height to 0dp and the weight to 1. Find the code below- also android:layout_gravity="center_vertical" is used to align the buttons in a row in case they contain variable length text. Use of 0dp and weight it a pretty neat yet not so well known trick.

    <LinearLayout
     android:id="@+id/parent_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/button_bue_3d"
     android:orientation="vertical" >
    
                <LinearLayout
                    android:id="@+id/layout_row1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
    
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/button1"
                        style="?android:attr/buttonStyleSmall"
                       android:layout_height="wrap_content"
                       android:layout_width="0dp"
                       android:layout_weight="1"
                        android:clickable="false"
                       android:layout_gravity="center_vertical"
                        android:text="ssssssssssssssssssssssssss" />
    
                    <Button
                        android:id="@+id/button2"
                        style="?android:attr/buttonStyleSmall"
                        android:clickable="false"
                        android:layout_height="wrap_content"
                         android:layout_width="0dp"
                       android:layout_weight="1"
                       android:layout_gravity="center_vertical"
                        android:text="sggggggg" />
    
    
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/layout_row2"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                      android:layout_height="0dp"
    
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/button3"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_height="wrap_content"
                         android:layout_width="0dp"
                       android:layout_weight="1"
                        android:layout_gravity="center_vertical"
                        android:text="s" />
    
                    <Button
                        android:id="@+id/button4"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_height="wrap_content"
                         android:layout_width="0dp"
                       android:layout_weight="1"
                        android:clickable="false"
                         android:layout_gravity="center_vertical"
                        android:text="s" />
    
    
                </LinearLayout>
    
    
           </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 10:13

    I wish this helps some one

    Result

    Code:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:grid="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 0x0"
            grid:layout_column="0"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="0"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 0x1"
            grid:layout_column="1"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="0"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 0x2"
            grid:layout_column="2"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="0"
            grid:layout_rowWeight="1" />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 1x0"
            grid:layout_column="0"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="1"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 1x1"
            grid:layout_column="1"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="1"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 1x2"
            grid:layout_column="2"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="1"
            grid:layout_rowWeight="1" />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 2x0"
            grid:layout_column="0"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="2"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 2x1"
            grid:layout_column="1"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="2"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 2x2"
            grid:layout_column="2"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="2"
            grid:layout_rowWeight="1" />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 3x0"
            grid:layout_column="0"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="3"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 3x1"
            grid:layout_column="1"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="3"
            grid:layout_rowWeight="1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="item 3x2"
            grid:layout_column="2"
            grid:layout_columnWeight="1"
            grid:layout_gravity="center"
            grid:layout_row="3"
            grid:layout_rowWeight="1" />
    
    </android.support.v7.widget.GridLayout>
    
    0 讨论(0)
  • 2020-11-22 10:14

    You can make this lot faster by overriding ViewGroup onLayout method. This is my universal solution:

    package your.app.package;
    
    import android.content.Context;
    import android.view.ViewGroup;
    
    public class GridLayout extends ViewGroup {
    
        public GridLayout(Context context) {
            super(context);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            final int columns = 2;//edit this if you need different grid
            final int rows = 2;
    
            int children = getChildCount();
            if (children != columns * rows)
                throw new IllegalStateException("GridLayout must have " + columns * rows + " children");
    
            int width = getWidth();
            int height = getHeight();
    
    
            int viewWidth = width / columns;
            int viewHeight = height / rows;
    
            int rowIndex = 0;
            int columnIndex = 0;
    
            for (int i = 0; i < children; i++) {
                getChildAt(i).layout(viewWidth * columnIndex, viewHeight * rowIndex, viewWidth * columnIndex + viewWidth, viewHeight * rowIndex + viewHeight);
                columnIndex++;
                if (columnIndex == columns) {
                    columnIndex = 0;
                    rowIndex++;
                }
            }
        }
    
    }
    

    EDIT: Don't forget match_parent for children!

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    0 讨论(0)
  • 2020-11-22 10:14

    Here you are :

    Button button = new Button(this);
    // weight = 1f , gravity = GridLayout.FILL 
    GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
                GridLayout.UNDEFINED,GridLayout.FILL,1f),
                GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
    // Layout_height = 0 ,Layout_weight = 0
    params.height =0;                                                                                                           
    params.width = 0;
    button.setLayoutParams(param);
    
    0 讨论(0)
  • 2020-11-22 10:15

    Here is what I did and I'm happy to say that this worked for me. I too wanted a 2x2, 3x3 etc. grid of items to cover the entire screen. Gridlayouts do not adhere to the width of the screen. LinearLayouts kind of work but you cant use nested weights.

    The best option for me was to use Fragments I used this tutorial to get started with what I wanted to do.

    Here is some code:

    Main Activity:

    public class GridHolderActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main_6);
        }
    }
    

    activity_main_6 XML (inflates 3 fragments)

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <fragment
            android:id="@+id/frag1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:name=".TwoHorizontalGridFragment"
            tools:layout="@layout/two_horiz" />
        <fragment
            android:id="@+id/frag2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:name=".TwoHorizontalGridFragment"
            tools:layout="@layout/two_horiz" />
        <fragment
            android:id="@+id/frag3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:name=".Grid.TwoHorizontalGridFragment"
            tools:layout="@layout/two_horiz" />
    

    Base fragment layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="match_parent">
    
        <ImageQueue
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/img1"
            android:layout_weight="1"/>
    
    
        <ImageQueue
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/img2"
            android:layout_weight="1"/>
    </LinearLayout>
    

    Fragment Class (only handles initialization of a custom view) inflates 2 tiles per fragment

    public class TwoHorizontalGridFragment extends Fragment {
    private View rootView;
    
    private ImageQueue imageQueue1;
    private ImageQueue imageQueue2;
    
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        /**
         * Inflate the layout for this fragment
         */
        rootView = inflater.inflate(
                R.layout.two_horiz, container, false);
        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        imageQueue1 = (ImageQueue)rootView.findViewById(R.id.img1);
        imageQueue2 = (ImageQueue)rootView.findViewById(R.id.img2);
        imageQueue1.updateFiles();
        imageQueue2.updateFiles();
    }
    

    }

    Thats it!

    This is a weird work around to using nested weights, essentially. It gives me a perfect 2x3 grid that fills the entire screen of both my 10 inch tablet and my HTC droid DNA. Let me know how it goes for you!

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

    Starting in API 21 without v7 support library with ScrollView:

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
        <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="2"
                >
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:layout_gravity="fill_horizontal"
                android:background="@color/colorAccent"
                android:text="Tile1" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:layout_gravity="fill_horizontal"
                android:background="@color/colorPrimaryDark"
                android:text="Tile2" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:layout_gravity="fill_horizontal"
                android:background="@color/colorPrimary"
                android:text="Tile3" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:layout_gravity="fill_horizontal"
                android:background="@color/colorAccent"
                android:text="Tile4" />
    
        </GridLayout>
    </ScrollView>
    
    0 讨论(0)
提交回复
热议问题