2012-12-24 51 views
0

我想在xml中并排绘制三个正方形,以便它们水平填充屏幕(每边减去12 dp的边距)。根据this post似乎可以做这与表格布局混乱,但我想知道是否有更好的方法。这是我尝试使用嵌套LinearLayouts,这将矩形垂直地填满整个屏幕但除此之外没有什么我在寻找:Android - 在xml中绘制正方形

<?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:layout_margin="12dp" 
    android:background="#ffffff" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
</LinearLayout> 
+1

父的LinearLayout应当包括机器人:wieghtSum =“3” –

+0

按照[文档】( http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum)android:weightSum“定义了最大权重和,如果未指定,则通过添加所有子项的layout_weight来计算总和“。所以这不会有什么区别。 –

回答

0

它似乎是最好的解决方案是用创建自定义视图分量的重写onMeasure()

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
} 
1

尝试改变三个内部linearlayouts'机器人:layout_width =‘match_parent’到“包装内容”。

我只是用下面的xml代码做了一个测试。请注意,您的版本有两个变化:

1我将match_parent更改为三个子布局的wrap_content。 2因为我没有背景图片,所以我只是使用三种不同的颜色作为背景。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="12dp" 
    android:background="#ffffff" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#ff121212" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="#ff676767" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="#ffababab" > 
    </LinearLayout> 
</LinearLayout> 

我得到这个画面:

my test result

因此,如果已经改变match_parent为wrap_content但不行,我猜你的背景图片太大。

此外,转向了解layout_weight。 Layout_weight不决定元素的大小,它只在每个元素需要父级布局的必要大小之后才起作用,然后父级布局根据layout_weight为每个元素分配其余大小。

+0

这完全没有什么,因为layout_weight已经在所有矩形的水平方向上设置了。即使layout_weight不在那里,也会水平地折叠所有的矩形,而不是制作方形。 –

+0

请看我更新的答案。 – TieDad

+0

我的背景图片是一个矩形。但是,图像的大小并不重要,因为它会扭曲填充布局。在这里,布局是矩形的,所以背景图像不管是什么尺寸都不会是方形的。 –

0

尝试用这种方式来获得边距和高度。

<?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:background="#ffffff" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_marginLeft="12dp" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:layout_marginRight="12dp" 
     android:background="@drawable/rectangle" > 
    </LinearLayout> 
</LinearLayout> 
+0

因为没有内容需要垂直换行,所以这只是将我的矩形折叠为4dp的水平线。此外,将边距放入子布局中功能上会更改UI,因为它会使父LinearLayout填充整个屏幕。 –