一、简述
- 六大基本布局(布局可以理解为画布),View(主件)被ViewGroup(主件的群)所继承。
- 按键,文本等都是放在布局上的
二、具体实现
- 默认用的是相对布局 RelativeLayout
- 布局,格式
布局,文本框,按键
<RelativeLayout 代码内容,设置相关参数> //开头
</RelativeLayout> //结尾 多了一个/
<TextView //文本框
/>
<Button //按键
/>
- 代码意思
每个布局都有的3句,背景,宽,高,id
xmlns:android="http://schemas.android.com/apk/res/android" //版本
xmlns:tools="http://schemas.android.com/tools" //工具位置
tools:context=".MainActivity"
android:background="#ff0000" //布局背景,可以是颜色,可以是图片
android:background="@drawable/bx1" //图片放在res下的drawable-hdpi 高分辨率
android:layout_width="500dp" //宽,match_parent跟屏幕一样宽,wrap_content图片有多大就显示多大
android:layout_height="200dp" //高,match_parent跟屏幕一样高
android:id="@+id/bx1" //创建id
android:layout_below="@id/bx1" //显示在bx1的下面
三、代码演示
- 一个布局下放文本框和按键
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="500dp"
android:layout_height="200dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ff0000"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小伟" android:id="@+id/textView1"/>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"/>
</RelativeLayout>
- 显示2张图片,一个大布局下放2个小布局,布局1创建id,布局2显示在布局1的下方(不创id,会盖住,只显示出来一个图)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/bx1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@drawable/bx1"
></RelativeLayout>
<RelativeLayout
android:layout_below="@id/bx1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@drawable/bx2"
></RelativeLayout>
</RelativeLayout>
师承上官可编程 —— 陈立臣