2017-04-05 151 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