简体   繁体   中英

How to set Colspan in Gridlayout using LinearLayout Child elements programmatically?

I am currently trying to create a Windows 8 Start Menu like GUI for my Android Application, showing different blocks that can be touched. For this purpose, I am using a GridLayout and keep on adding child elements in a for-loop from an xml file. So far, everything is working fine. My problem is, that I implemented different sizes of the tiles, such that there exist normal (quadratic) ones and ones that are twice the size in horizontal stretch. These bigger ones are displayed properly, but due to the lacking colSpan there is a white gap above and below the bigger tiles.

II II II II    II II II
II II II XXXXX II II II
II II II II    II

I am sorry, I can't add images yet, but above and below the bigger tile (XXXX) there is space that could be filled with another tile to make the whole thing better look like:

II II II II II II II II
II II II XXXXX II II II
II II II II II

This is my code when adding the tiles:

// GridLayout design
GridLayout gl = (GridLayout) findViewById(R.id.main_grid_layout);


// Kachel design
LinearLayout kachel = new LinearLayout(this);
kachel.setBackgroundResource(getBackgroundOfCategory(kategorieid));
kachel.setOrientation(LinearLayout.VERTICAL);

// Small and bigger kacheln
LinearLayout.LayoutParams kachelLP = new LinearLayout.LayoutParams(320,320);
if(size == 1) {
    kachelLP = new LinearLayout.LayoutParams(640,320);   
}

kachelLP.setMargins(10,10,10,10);
kachel.setLayoutParams(kachelLP);
kachel.setClickable(true);
// Kachel actions
kachel.setOnClickListener(new KachelOnClickListener(kachelid, getResources(), getBaseContext()));

...

Any help is highly appreciated!

You would need to use columnspec attribute of GridLayout's LayoutParam. or you could define the colspan in xml(android:layout_columnSpan="2") incase your requirements are static.

Please find the same here. http://developer.android.com/reference/android/widget/GridLayout.LayoutParams.html

Here's a related question. Set rowSpan or colSpan of a child of a GridLayout programmatically?

Further help can be found here. http://android-developers.blogspot.in/2011/11/new-layout-widgets-space-and-gridlayout.html

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM