1. 程式人生 > >橫向、縱向充滿螢幕(4個按鈕正好各佔1/4螢幕面積(居中))——田字格

橫向、縱向充滿螢幕(4個按鈕正好各佔1/4螢幕面積(居中))——田字格

田字格 amxl

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/main_bg">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:orientation="vertical">
        <Button
            android:text="Button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:id="@+id/button1" />
        <Button
            android:text="Button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:id="@+id/button2" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:orientation="vertical">
        <Button
            android:text="Button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:id="@+id/button3" />
        <Button
            android:text="Button4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:id="@+id/button4" />
    </LinearLayout>
</LinearLayout>

android:layout_weight="50" 權重屬性

注意權重屬性就行啦,比如在LinearLayout 內一個權重是 70 一個權重是30 那麼權重70的比30的寬按比例寬的

效果圖:


使用權重設定分屏佔用比例如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/FrameLayout1">
        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2">
            <Button
                android:text="Button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/button1" />
        </LinearLayout>
    </FrameLayout>
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/FrameLayout2">
        <LinearLayout
            android:id="@+id/LinearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true">
            <Button
                android:id="@+id/Button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="Button2"
                android:background="#00ffff"
                android:layout_marginRight="1dp"
                android:textColor="#000000"
                android:layout_weight="1"
                android:textSize="50px" />
            <Button
                android:id="@+id/Button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="Button3"
                android:background="#00ffff"
                android:layout_weight="1"
                android:layout_marginLeft="0.0dp"
                android:textColor="#000000"
                android:textSize="50px" />
        </LinearLayout>
    </FrameLayout>
</LinearLayout>