一、lineralayout
1、LineraLayout控件下的属性
外边距 margin(控件和父容器的距离)
内边距 padding(内容和控件的距离)
外边距和内边距可以参照盒子模型
2、RGB颜色
红色 绿色 蓝色
FF0000 00FF00 0000FF
注意点:一般可以使用前需带#,控制控件颜色的一般方法:红色:#FF0000,红色变浅#□□FF0000,□□越大,表示颜色越深,越小,表示颜色越浅
3、尺寸单位(dp、sp)
dp控件尺寸、间距的单位
sp字体大小尺寸
例子:登录界面源代码:
<?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="@drawable/bg2"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0sp"
android:text="姓名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:paddingLeft="5.0dp"
android:paddingTop="5.0dp"
android:paddingBottom="5.0dp"
android:hint = "请输入姓名"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0sp"
android:textColor="#212121"
android:text="密码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:paddingLeft="5.0dp"
android:paddingTop="5.0dp"
android:paddingBottom="5.0dp"
android:hint = "请输入密码"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dp"
android:layout_marginRight="10.0dp"
android:paddingLeft="20.0dp"
android:paddingRight="20.0dp"
android:background="#66404040"
android:text="注册"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dp"
android:layout_marginRight="10.0dp"
android:background="#66404040"
android:paddingLeft="20.0dp"
android:paddingRight="20.0dp"
android:text="登陆" />
</LinearLayout>
</LinearLayout>
二、RalativeLayout
控件在相对布局中是自由的,可以设置在任何位置,所以说相对布局是最灵活的布局。
1、实现居中对齐
android:layout_centerHorizontal="true"//水平居中
android:layout_centerVertical="true"//垂直居中
android:layout_centerInParent="true"//水平和垂直居中,就在容器的正中间
2、实现在父容器里的边缘对齐
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
2、实现控件与控件之间的位置关系(控件要设置id)
android:layout_toLeftOf ="@id/~~~~"//在某个控件的左边
android:layout_toRightOf="@id/~~~~" //在某个控件的右边
android:layout_above ="@id/~~~~" // 在某个控件的上边
android:layout_below ="@id/~~~~" //在某个控件的下边
3、控件与控件之间的边缘的位置关系(控件要设置id)
android:layout_alignTop="@id/~~~~"//与id为···的控件等高的位置
android:layout_alignBottom="@id/~~~~"//与id为·····的控件等底的位置
android:layout_alignLeft="@id/~~~~"//与id为·····的控件等左边的位置
android:layout_alignRight="@id/~~~~"//与id为·····的控件等右边的位置
例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="@string/re_above"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/re_behind" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/re_left"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/re_right"
/>
<Button
android:id="@+id/btn_center"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="杏黃旗"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_center"
android:layout_alignTop="@id/btn_center"
android:text="龙吉"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_center"
android:layout_alignBottom="@id/btn_center"
android:text="龙须虎"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/btn_center"
android:layout_alignLeft="@id/btn_center"
android:text="金吒"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_center"
android:layout_alignRight="@id/btn_center"
android:text="银吒"
/>
</RelativeLayout>
效果如图