1. 程式人生 > >Android線性佈局五角示例

Android線性佈局五角示例

LinearLayout線性佈局

元素屬性:

                  gravity="left"(是本元素所有子元素的對齊方式,設定在父元素上,可設定多個值,多個值用|符號隔開

                  layout_gravity (子元素在父元素的對齊方式,設定在子元素上)

                  orientation="vertical"  時, 只有水平方向的設定才起作用。垂直分佈
                  orientation="horizontal" 時, 只有垂直方向的設定才起作用。水平分佈

                  wrap_parent:匹配自身長寬

                  match_parent:匹配父長寬,也就是整個顯示螢幕

                  fill_parent:填充螢幕長寬

  實現關鍵點:(線性佈局巢狀,layout_weight權重比)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.10"
        android:gravity="left"
        android:orientation="vertical"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕2"
            android:layout_marginTop="400dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2.49"
        android:gravity="center"
        android:orientation="vertical"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕3"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="0.33"
        android:gravity="right"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕4"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕5"
            android:layout_marginTop="400dp"/>

    </LinearLayout>

</LinearLayout>

 注:在使用layout_weight權重分比時,多注意值大小的分配。