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