第3章软件也要拼脸的 3.3详解4种基本布局

来源:互联网 发布:新业汽修软件视频 编辑:程序博客网 时间:2024/05/18 00:01

详解4种基本布局
1.线性布局LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <EditText        android:id="@+id/input_message"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="3"        android:hint="Type something"/>    <Button        android:id="@+id/send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Send"/>    <!--<Button-->        <!--android:id="@+id/button2"-->        <!--android:layout_width="wrap_content"-->        <!--android:layout_height="wrap_content"-->        <!--android:layout_gravity="center_vertical"-->        <!--android:text="Button 2"/>-->    <!--<Button-->        <!--android:id="@+id/button3"-->        <!--android:layout_width="wrap_content"-->        <!--android:layout_height="wrap_content"-->        <!--android:layout_gravity="bottom"-->        <!--android:text="Button 3"/>--></LinearLayout>

2.相对布局RelativeLayout
相对于父布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="Button 1"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:text="Button 2"/>    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Button 3"/>    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:text="Button 4"/>    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="Button 5"/></RelativeLayout>

相对于控件定位

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Button 3"/>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/button3"        android:layout_toLeftOf="@id/button3"        android:text="Button 1"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/button3"        android:layout_toRightOf="@id/button3"        android:text="Button 2"/>    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button3"        android:layout_toLeftOf="@id/button3"        android:text="Button 4"/>    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button3"        android:layout_toRightOf="@id/button3"        android:text="Button 5"/></RelativeLayout>

祯布局FrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/text_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="left"        android:text="This is TextView"/>    <ImageView        android:id="@+id/image_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:src="@mipmap/ic_launcher"/></FrameLayout>

百分比布局

<android.support.percent.PercentFrameLayout    xmlns:android ="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button1"        android:text="Button 1"        android:layout_gravity="left|top"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        />    <Button        android:id="@+id/button2"        android:text="Button 2"        android:layout_gravity="right|top"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        />    <Button    android:id="@+id/button3"    android:text="Button 3"    android:layout_gravity="left|bottom"    app:layout_widthPercent="50%"    app:layout_heightPercent="50%"    />    <Button        android:id="@+id/button4"        android:text="Button 4"        android:layout_gravity="right|bottom"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        /></android.support.percent.PercentFrameLayout>
原创粉丝点击