@1 第一种是FrameLayout布局。这种布局方式是一种堆叠的方式,最先摆在布局文件中的会在最底层显示。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is a"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is b"/>
</FrameLayout>
文件 this is b 会显示在this is a 的上面。
@2 第二种是LinearLayout布局。它分为水平和垂直布局,由orientation属性设置。其可以设置gravity属性控制视图内容的对齐方式。layout_gravity属性用来控制视图相对其父视图的对齐位置。可以设置多个并用"|"分离不能有空格。其布局文件中的视图还可以用layout_weight属性来进一步设置视图所占比例。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is b"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is b"/>
</LinearLayout>
</LinearLayout>
//
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="this is a"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:gravity="right"
android:background="@color/colorPrimary"
android:text="this is b"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is b"/>
</LinearLayout>
</LinearLayout>