Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 常用布局小結:FrameLayout、LinearLayout、RelativeLayout 與漸變背景

編輯:關於 Android 程式設計

1. FrameLayout(幀布局)

效果是多個圖層依次疊加,像畫一幅畫:先放一張背景 ImageView,再往上疊其他元素。

如果想讓三個按鈕分別在水平方向左對齊、居中、右對齊,可以給每個子 View 單獨設置 layout_gravity:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="btn1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="btn2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="btn3" />
</FrameLayout>

要點在於 FrameLayout 的所有子 View 默認都從左上角開始擺放,全靠 gravity 把它們推向不同位置,因此各元素之間不存在相互影響。

2. LinearLayout(線性布局)

靠 android:orientation 決定排列方向。若要在水平或垂直方向上按比例分配空間,則給子 View 設置 layout_weight,剩餘空間會按權重劃分。

3. RelativeLayout(相對布局)

允許子 View 相對於父容器或其他兄弟 View 定位,是層級最淺、性能較好的選擇之一。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_toRightOf="@+id/btn1"
        android:text="btn2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn2"
        android:layout_toRightOf="@+id/btn2"
        android:text="btn3" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_toLeftOf="@+id/btn2"
        android:text="btn4" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_toRightOf="@+id/btn2"
        android:text="btn5" />
</RelativeLayout>

常用屬性可以歸成兩類:一組是 layout_above / below / leftOf / rightOf,相對於兄弟定位;另一組是 layout_alignParentTop / Bottom / CenterInParent 等,相對於父容器定位。注意被引用的 View 必須先出現在布局中,或者用 @+id/ 提前聲明。

如何動態改變控件在布局中的位置

通過 LayoutParams 實現:

btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (i == 1) {
            i = 0;
            lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        } else {
            i = 1;
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        }
        btn.setLayoutParams(lp);
    }
});

關鍵是 setLayoutParams() 之後會觸發重新布局,位置即刻生效。

4. 設置漸變背景色

用 GradientDrawable,構造時傳入漸變方向與顏色數組即可:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int[] col = {Color.BLUE, Color.BLACK};
    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, col);
    getWindow().setBackgroundDrawable(gd);
}

除了代碼方式,也可以寫成 XML 放在 res/drawable/ 下再用 android:background="@drawable/xxx" 引用,這種做法更易於維護和調色:

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:startColor="#0000FF"
        android:endColor="#000000"
        android:type="linear" />
</shape>

注意 XML 中的 angle 必須是 45 的倍數,且 0 度指向東方,與代碼裡的 Orientation 枚舉方向相反,這一點常讓人困惑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved