2016-01-20 69 views
2

我正在尝试做一个计时器应用程序的实践,我遇到了一个问题,其中gridview没有与其他大小相同的大小。android gridview等大小的单元

这是代码

<GridLayout 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/passwordField" 
    android:columnCount="3" 
    android:rowCount="4" 
    android:verticalSpacing="0dp" 
    android:horizontalSpacing="0dp" 
    android:stretchMode="columnWidth"> 


    <Button 
     android:id="@+id/btnOne" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" 
     android:onClick="addInput" 
     android:text="@string/one" /> 

    <Button 
     android:id="@+id/btnTwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" 
     android:onClick="addInput" 
     android:text="@string/two" /> 

    <Button 
     android:id="@+id/btnThree" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="2" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" 
     android:onClick="addInput" 
     android:text="@string/three" /> </GridLayout> 

有后多个按钮的一部分,但是这是所有我需要表明现在 你可以从图像中看到: enter image description here

它没有相同的单元格大小,其中第3列比其他列大。 我该如何解决这个问题?

我有RelativeLayout之前与正确的地方的一切,但按钮没有伸出来填补宽度。

+0

您可以尝试使用机器人:在按钮的观点重量属性等于体重/大小分配给所有的意见 – AndRSoid

+0

更改网格布局宽度来包装内容 –

+1

只有用于api 22及以上的layout_columnweight是你的意思吗? –

回答

1

layout_columnWeight是缺少的部分。请注意,您可以简化按钮声明,以及:

<GridLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/passwordField" 
     android:columnCount="3" 
     android:rowCount="4" 
     android:verticalSpacing="0dp" 
     android:horizontalSpacing="0dp"> 


     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="addInput" 
      android:text="@string/one" 
      android:layout_columnWeight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:onClick="addInput" 
      android:text="@string/two" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:onClick="addInput" 
      android:text="@string/three" /> 

    </GridLayout> 
+0

的layout_columnweight,之前我确实尝试过,但没有真正改变。我结束了用android:layout_columnWeight =“1”改变所有的按钮,并得到它的工作,我想我将不得不将它应用到此gridlayout中的每个按钮。 –

+0

是的,你会需要它的所有儿童意见。对于“0”按钮,您可以使用layout_columnweight =“3”跨越整个宽度。 – davehenry

2

将此属性的每个按钮XML的

 android:layout_columnWeight="1"