2017-04-05 145 views
0

我试图配置一个2行2列的网格布局。但是这里的每个元素占据屏幕的整个宽度,而第二列在屏幕之外。请建议我在这里做错了什么。GridLayout列没有显示正确

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

    <GridLayout 
     android:id="@+id/moodsGridLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:columnCount="2" 
     android:orientation="vertical" 
     android:rowCount="2"> 

     <ImageButton 
      android:id="@+id/imageButton" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_row="0" 
      app:srcCompat="@mipmap/brewtv_logos" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_row="0" 
      app:srcCompat="@mipmap/brewtv_logos" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_row="1" 
      app:srcCompat="@mipmap/brewtv_logos" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_row="1" 
      app:srcCompat="@mipmap/brewtv_logos" /> 
    </GridLayout> 

</LinearLayout> 
+0

对我来说,这似乎很明显,如果一个列的宽度设置为'match_parent',其他列将没有剩余空间。尝试使用您的ImageButtons的dicrete dp值。 –

+0

由于元素被包含在GridLayout中,并且每个底层元素的行跨度仍然为1,因此它应该只占用一半的屏幕,对吧?如果我错了,请纠正我。 – jay

+0

尝试。也许我错了。 –

回答

2

尝试这种情况:

为均匀的宽度的列和行中的GridLayout使用:

android:layout_columnWeight="1"  
android:layout_rowWeight="1" 

使用这种布局:

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

    <GridLayout 
     android:id="@+id/moodsGridLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/textView2" 
     android:columnCount="2" 
     android:orientation="vertical" 
     android:rowCount="2"> 

     <ImageButton 
      android:id="@+id/imageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:layout_row="0" 
      android:layout_rowWeight="1" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="1" 
      android:layout_row="0" 
      android:layout_rowWeight="1" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:layout_row="1" 
      android:layout_rowWeight="1" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="1" 
      android:layout_row="1" 
      android:layout_rowWeight="1" 
      android:src="@mipmap/ic_launcher" /> 
    </GridLayout> 

</LinearLayout> 

输出:

enter image description here