安卓学习- 编写界面(页面布局详解)

来源:互联网 发布:淘宝上很火的安娜家 编辑:程序博客网 时间:2024/06/02 08:04
1、线性布局
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>
</LinearLayout>


(1)排列方向:
android:orientation="horizontal" 水平
android:orientation="vertical" 垂直

(2)元素布局的对其方式:
对其方式,可选项为top、bottom、left、rirht、center,
中间可以用 | 来指定多个;
android:layout_gravity="bottom"
注意:如果是vertical,就只有水平上的对齐才会生效

(3)边距
android:layout_marginTop="10dp"

(4)比例权重
android:layout_weight="1"
在某一个方向上设置权重之后,原本的那个方向上的数量置为0dp
<Button
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button 1"
/>
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
/>


2、相对布局
(1)相对父布局
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="Button 1"
/>
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"

android:layout_centerInParent="true"

(2)相对控件布局
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3"
/>

<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="Button 1"
/>

android:layout_below="@id/button_3"
android:layout_toRightOf="@id/button_3"




3、帧布局(略过)
FrameLayout

4、百分比布局(新增布局)(PercentFrameLayout,PercentRelativeLayout)
打开app/build.gradle,在dependencies闭包中添加如下内容:
dependencies {
compile fileTree(dir:'libs',include: ['*.jar'])
testCompile'junit:junit:4.12'
compile'com.android.support:appcompat-v7:24.2.1'
compile'com.android.support:percent:24.2.1'
}

然后在布局中使用 PercentFrameLayout
<?xml version="1.0"encoding="utf-8"?>
<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>