1

我想在中間的兩行和兩列有一個頁眉和頁腳以及四張圖片(ImageView)的佈局。我不想使用GridView。Android:如何在xml中平衡GridLayout

這就是我所想的:

Header and footer with block of four ImageViews in center (they are currently buttons)

的網格佈局元素走自己的路,大小和不縮水被周圍的元素所規定的尺寸。它們要麼變得太大,要麼脫離頁面,要麼被以下LinearLayout裁剪掉。這裏是我的意思的例子:我想你需要看到XML

Screenshot on Nexus 5

<?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" 
android:weightSum="3" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <GridLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_column="1" 
      android:layout_columnSpan="2" 
      android:layout_gravity="fill_vertical|fill_horizontal" 
      android:layout_row="0" 
      android:layout_weight="1" 
      android:columnCount="2" 
      android:weightSum="4" 
      android:rowCount="2" > 

    <Button 
     android:id="@+id/button2" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:layout_gravity="fill_vertical|fill_horizontal" 
    android:layout_weight="1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button13" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:layout_gravity="fill_vertical|fill_horizontal" 
    android:layout_weight="1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button14" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_gravity="fill_vertical|fill_horizontal" 
    android:layout_weight="1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button15" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:layout_gravity="fill_vertical|fill_horizontal" 
    android:layout_weight="1" 
     android:text="Button" /> 

     </GridLayout> 
    </LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" > 
</LinearLayout> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
    </LinearLayout> 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

我想:

  • 頁眉和頁腳
  • 四個大小相同的圖片
  • 左右邊界在四張圖片的周圍。

也許我對此採取了錯誤的做法,所以如有必要請隨時以新的方向發送給我。

+0

你的意思是你想在中心沒有使用gridview的四個相同大小的圖像? – 2014-11-01 04:08:58

+0

是的,沒錯,沒有GridView。 GridView似乎是爲圖像列表製作的,就像一個相冊,它有一個我不想要的動作,當你拖動它時。 – grooble 2014-11-01 04:25:34

+0

然後如果可能的話,把你所有的圖像視圖放在一個線性佈局中,並將重力作爲整個佈局的中心。 – 2014-11-01 04:31:34

回答

1

沒有GridView你可以嘗試:

它可以完美運行。

<?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" 
    android:weightSum="3" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="fill_vertical|fill_horizontal" 
        android:layout_row="0" 
        android:layout_weight="1" 
        android:text="Button" /> 

       <Button 
        android:id="@+id/button13" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="fill_vertical|fill_horizontal" 
        android:layout_row="0" 
        android:layout_weight="1" 
        android:text="Button" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/button14" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="fill_vertical|fill_horizontal" 
        android:layout_weight="1" 
        android:text="Button" /> 

       <Button 
        android:id="@+id/button15" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="fill_vertical|fill_horizontal" 
        android:layout_weight="1" 
        android:text="Button" /> 
      </LinearLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 
    </LinearLayout> 

</LinearLayout> 
+0

是的,你是對的。它按照我想要的方式工作。也許GridLayout不用於表示對象的網格。多麼令人誤解的名字。如果沒有什麼比第二天使用GridLayout更好的話,我會接受。 – grooble 2014-11-01 04:50:00

+0

感謝Upvote ...你也可以接受:)快樂編碼。 – 2014-11-01 05:15:59