Android - 在xml中绘制正方形

时间:2012-12-24 06:14:45

标签: android

我想在xml中并排绘制三个正方形,以便它们水平填充屏幕(减去每边12 dp的边距)。根据{{​​3}},似乎可以使用表格布局来实现这一点,但我想知道是否有更好的方法。这是我尝试使用嵌套的LinearLayouts,它绘制了垂直填充整个屏幕的矩形,但另外做了我正在寻找的东西:

<?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>

3 个答案:

答案 0 :(得分:1)

尝试将三个内部线性布局'android:layout_width =“match_parent”更改为“wrap_content”。

我刚刚使用以下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为每个元素分配其余大小。

答案 1 :(得分: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>

答案 2 :(得分:0)

似乎最好的解决方案是使用覆盖的onMeasure()创建自定义视图组件:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
相关问题