1. 程式人生 > >Android Studio App設定線性佈局LinerLayout控制元件佔螢幕長寬比例

Android Studio App設定線性佈局LinerLayout控制元件佔螢幕長寬比例

如何設定兩個控制元件在水平方向上的佔螢幕的比例?

1.要有線性佈局LinerLayout

2.設定線性佈局控制元件水平排列:

android:orientation="horizontal"
3.設定第一個控制元件:
android:layout_width="0dp"
android:layout_weight="6"

第二個控制元件:

android:layout_width="0dp"
android:layout_weight="4"
這樣兩個控制元件佔螢幕比例分別為6:4. 整個佈局範例內容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:background="#1E90FF">
    <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        />
    <Button
        android:id="@+id/enter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:text="輸入"/>
</LinearLayout>
實際效果: