简体   繁体   English

如何以编程方式使用LinearLayout子元素在Gridlayout中设置Colspan?

[英]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. 我目前正在尝试为Android应用程序创建Windows 8开始菜单(如GUI),以显示可以触摸的不同块。 For this purpose, I am using a GridLayout and keep on adding child elements in a for-loop from an xml file. 为此,我正在使用GridLayout并继续从xml文件的for循环中添加子元素。 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. 这些较大的图块可以正确显示,但是由于缺少colSpan,因此在较大图块上方和下方均存在白色间隙。

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: 抱歉,我还不能添加图像,但是在较大图块(XXXX)的上方和下方,可以用另一个图块填充空间,以使整个外观看起来更好:

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. 您将需要使用GridLayout的LayoutParam的columnspec属性。 or you could define the colspan in xml(android:layout_columnSpan="2") incase your requirements are static. 如果您的需求是静态的,也可以在xml(android:layout_columnSpan =“ 2”)中定义colspan。

Please find the same here. 请在这里找到相同的。 http://developer.android.com/reference/android/widget/GridLayout.LayoutParams.html 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? 以编程方式设置GridLayout的子级的rowSpan或colSpan?

Further help can be found here. 可以在此处找到更多帮助。 http://android-developers.blogspot.in/2011/11/new-layout-widgets-space-and-gridlayout.html http://android-developers.blogspot.in/2011/11/new-layout-widgets-space-and-gridlayout.html

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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