在之前对布局文件前先对布局问建用的xml文件结构进行了解,以便知道其中的意识:
xml文件 与 html文件类似,是静态网页文件。xml是可扩展标记类语言,可以自定义标签,标签和html一样是成对出现的,如<id>......</id> 或者<id....../> 。 xml是存储传输数据,而html只是显示其内容。
注意在工程中res文件夹下的所有的文档都是xml格式的。
布局的xml一般放在res->layout文件夹下。
线型布局:LinearLayout
线型布局文件其放置格式有水平布局(horizontal)和垂直布局(vertical),其使用格式如下:
android:orientation="vertical"或android:orientation="horizontal"
布局的长、宽格式:android:layout_weight=" "或android:layout_height=" " 其" "中可以选择fill_parent 、match_parent 或wrap_content,其中前两个功能都差不多,为填充满父容器,而wrap_content则是包裹满类容;当布局中android:layout_weight="1"时,则是将布局进行比例分配。
布局中android:background=" "则是设置背景颜色或者背景图案。
注意:调用系统内容是要用@引用
实现如下布局:
其xml布局如下:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:orientation="horizontal" >
<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="按键3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按键5" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按键2" />
<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="按键6" />
</LinearLayout>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:textSize="24dp"
android:text="你好" />
</LinearLayout></span>
实现比例分配如下所示内容:
其xml代码如下:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff669900"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮6" />
</LinearLayout>
</LinearLayout></span>
上述两个布局中android:background="#ff669900"是自定义的背景颜色;而android:background="@android:color/holo_orange_dark"是调用的系统颜色。
当然掉用的时候要在相关的Java文件中用setContentView(xml布局文件在R文件下的id地址)建立联系才能使用。