GridLayout (not GridView) how to stretch all children evenly

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

    
    
    
    
        
        
        
    

    Base fragment layout

    
    
        
    
    
        
    
    

    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!

提交回复
热议问题