首先先看下布局成果
界面的设置都是利用UI布局来进行实现
图片背景的设计,加在mipmap中
先来看看上面代码:
<?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"
android:background="@mipmap/timg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/qq"
android:text="QQ"
android:textSize="40sp" />
<!---只能数字登录-->
<EditText
android:id="@+id/editText2"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_width="match_parent"
android:layout_height="64dp"
android:hint="QQ号码/手机/邮箱"
android:inputType="numberSigned" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:ems="10"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background=" #ffe5e5"
android:text="登录"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forget"
android:textColor="#1ad1ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/creat"
android:textColor="#1ad1ff" />
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:textColor="#222222"
android:text="@string/read"
android:textSize="15sp"/>
</RelativeLayout>
线性布局:
1、我们可以通过orientation这个属性来修改LinearLayout布局的孩子摆放位置
- vertical 垂直
- horizontal 水平
2、线性布局中的权重(weight)
- 当有些时候,我们需要平均的给孩子宽度或者高度,这个时候我们可以用权重
- 有时候不平均,但是宽度/高度成比例
相对布局:
- 1、 相对于父控件来说
- 2、相对于同级控件来说
1、相对于父控件来说:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button4"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/middle" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/leftup" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/leftdown" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/rightup" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/rightdown" />
</RelativeLayout>
效果展示:
2、相对于同级控件来说
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--这个是中间-->
<Button
android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/middle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/center"
android:text="@string/left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/center"
android:text="@string/right"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/center"
android:layout_centerHorizontal="true"
android:text="@string/left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center"
android:layout_centerHorizontal="true"
android:text="@string/down"/>
其他布局:
- 绝对布局(AbsoluteLayout):利用xy来控制位置
- 表格布局(TableLayout )
- 帧布局(FrameLayout)
布局的常用单位
- 像素单位:px,也就是像素(不建议使用),除非是手表,或者机顶盒
- dp | dip(密度 独立 像素)缩写(推荐使用),适配屏幕的单位
- 字体单位:sp,和dp一样,可以根据用户的字体大小首选项进行缩放,主要用来处理字体的大小