2014-02-11 84 views
1

我使用的GridLayout在Android应用中,但我有一个显示问题,的Android网格布局显示

我用setColumnCount有3列,因为我必须添加每行3个元素,所以它应该对齐。

layout1 = new GridLayout(this); 
layout1.setColumnCount(3); 

//In a loop later in the code : 
layout1.addView(textView1); 
layout1.addView(cb); 
layout1.addView(textView2); 

3项不与行对齐,但他们都在第一列,我真的不明白这个问题。

回答

1

使用的LayoutParams为您的孩子视图

GridLayout gridLayout = new GridLayout(this); 
gridLayout.setColumnCount(colCount); 
gridLayout.setRowCount(rowCount); 

GridLayout.LayoutParams third = new GridLayout.LayoutParams(0, 0); 
textView1.setLayoutParams(third); 
gridLayout.addView(textView1, third); 

GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(0, 1); 
cb.setLayoutParams(fourth); 
gridLayout.addView(cb, fourth); 

GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(0, 2); 
textView2.setLayoutParams(fifth); 
gridLayout.addView(textView2, fifth); 
+0

它不工作,月食说,构造函数不存在 – user3244162

+1

这是伪代码,尝试http://developer.android.com/reference /android/widget/GridLayout.LayoutParams.html ... –

+0

最后谢谢你的作品! – user3244162

0

应指定的细胞会在您的addView方法,查看。 This docGridLayout.addView可能会帮助你。

0

也许,如果它是静态的,它会永远是3项,你可以在水平方向的XML设置的LinearLayout。

0

你试过吗? :

<GridView 
     android:id="@+id/photo_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:numColumns="3" > 

</GridView> 

而且使1个自定义XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <ImageView 
     android:id="@+id/img_photo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:layout_margin="5dp" 
     android:src="@drawable/img_photo_caption" /> 

</LinearLayout>