Android Studio布局:尝试将按钮设置为50%宽度和50%高度,按钮位于中心

时间:2015-10-11 18:27:42

标签: android xml android-layout android-studio android-linearlayout

我在Android Studio工作,希望我的布局是一个2 x 2的按钮网格,中间有一个按钮。请参阅下面的代码。

#define DEBUG

...

#if DEBUG
    const string Version = "X.Y.Z";
#else
    const string Version = "A.B.C";
#end if

这就是它现在的样子:

enter image description here

我喜欢这种布局,但我希望按钮的高度为50%。有没有人有任何建议?

1 个答案:

答案 0 :(得分:3)

如果您正在寻找这样的设计

enter image description here

您可以使用FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2" >

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

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

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


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

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="3" />

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

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/instagram"/>

</FrameLayout>

我已将LinearLayoutweightSum一起使用。看看它是否有帮助!

相关问题