安卓app开发-05-Android xml布局详细介绍
- 虽然说有 墨刀,墨客 这些图形化开发工具来做 Android 的界面设计,但是我们还是离不开要去学习做安卓原生app,学习 xml 布局还是必要的
(1)准备
- 首先我们要了解 android 到底有那些布局,和每个布局类型的区别
- 学习时最好打开 Android Studio 打开 xml 文件对应看一下
- 配置参数的详细含义不用着急全部理解,放在文章后面,可收藏做查阅【可通过目录跳转】
(2)学习目标
- 学习下xml的布局文件具体写法。这一节我们要绘制如下图所示的界面
(3)线性布局 LinearLayout
- 线性布局分两种。一种是水平布局,一种是垂直布局。下面我们根据上图举例子
- 上图代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_height="wrap_content" android:text="@string/note_title" android:layout_width="wrap_content" android:padding="10dp"></TextView> <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"> <EditText android:id="@+id/EditText02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left" android:hint="@string/edithint"></EditText> </LinearLayout> <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="2" android:gravity="center" android:orientation="horizontal"> <ImageButton android:id="@+id/ImageButton01" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_003" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton02" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_004" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton03" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_007" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton04" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_011" android:layout_margin="3dp"></ImageButton> </LinearLayout> </LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 可以看到,上图是由三部分组成。在大的LinearLayout从上而下垂直分布着三个内容:TextView,LinearLayout,LinearLayout。所以总体的 LinearLayout 是垂直布局
- 下面我们来看水平布局
- 其实就是上图中的最下面那个 LinearLayout。四个图标平行排列。
android:orientation=“horizontal”
(4)相对布局 RelativeLayout
- 这个布局相对简单一点。一般来讲利用ADT自己拖放按钮就可以。基本上可以随意布局。如下图所示