LinearLayout布局
LinearLayout布局是一种线性布局,是Android开发中经常用到的一种布局,它可以嵌套多个LinearLayout使用,但是我们布局的时候要注意对布局属性的设置,不然就可能和预想中的效果有差异,有可能会出现覆盖掉的情况,这是需要我们注意的。
线性布局分为水平布局和垂直布局两种线性布局,是通过android:orientation来实现的,它代表控件的排列方向,“horizontal”代表水平布局,“vertical”代表垂直布局。
基本的布局属性还有android:layout_width设置布局宽度,android:layout_height设置布局高度,会用到wrap_content填满内容,是根据内容对高宽度的一个变化,match_parent是对父容器的一个高宽度的填满,当然也可以自定义高宽度;android:layout_weight属性是用来分配空间的一个属性,可以设置它的权重,比如一个LinearLayout布局中需要两个同样布局的效果,这个时候就要设置它的权重为1,如果是横向(水平)布局,就设置宽度为0dp,纵向(垂直)布局的话就设置高度为0dp,这个属性是我们经常会用到的,在设置布局时是很重要的。
还有布局的背景设置,内外边距的设置,以及布局的居中显示等。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorBlue">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorGreen">
</LinearLayout>
</LinearLayout>
RelativeLayout布局
Android布局主要的控件布局属性是我们要掌握的,也是我们经常用到的一些控件。
RelativeLayout布局:除了LinearLayout布局还有相对布局RelativeLayout,也是经常使用的布局,它是相对于某个控件的相对位置,允许子元素指定相对于父元素的位置,获取的是父元素id,根据它来设置子元素的位置,分为四种
android:layout_alignLeft 相对左边左对齐;
android:layout_alignRight 相对右边右对齐;
android:layout_alignTop 相对上边上放对齐;
android:layout_alignBottom 相对下边下边对齐;
也有相对于父容器的,值为false/true
android:layout_alignParentLeft 相对父容器左边;
android:layout_alignParentRight 相对父容器右边;
android:layout_alignParentTop 相对父容器上边;
android:layout_alignParentBottom 相对父容器下边;
android:layout_centerInParent 相对父容器整体居中;
android:layout_centerHorizontal 相对父容器水平居中;
android:layout_centerVertical 相对父容器垂直居中。
还有这几个属性:
android:layout_below 在某元素的下方;
android:layout_above 在某元素的上方;
android:layout_toLeftOf 在某元素的左边;
android:layout_toRightOf 在某元素的右边;
这都是相对布局中常用到的一些属性。
Android常用控件及其属性
1、TextView文本控件:主要是显示文本信息
<TextView
android:layout_width="wrap_content" //宽度
android:layout_height="wrap_content" //高度
android:text="测试" //文本
android:textSize="16sp" //字体大小
android:textColor="@color/colorBlue" //字体颜色
android:textStyle="normal" //字体格式
android:gravity="center" //字体位置
android:singleLine="true"/> //是否只在同一行显示
2、EditView文本输入框控件:可编辑的输入框
<EditView
android:layout_width="wrap_content" //宽度
android:layout_height="wrap_content" //高度
android:text="测试" //文本
android:textSize="16sp" //字体大小
android:textColor="@color/colorBlue" //字体颜色
android:textStyle="normal" //字体格式
android:gravity="center" //字体位置
android:singleLine="true" //是否只在同一行显示
android:inputType="textPassword" //文字的限制(密码,数字,字母)
android:hint="测试"/> //提示文本
3、Button按钮控件:按钮控件
<Button
android:layout_width="wrap_content" //宽度
android:layout_height="wrap_content" //高度
android:text="测试" //文本
android:textSize="16sp" //字体大小
android:textColor="@color/colorBlue" //字体颜色
android:textStyle="normal" //字体格式
android:gravity="center" //字体位置
android:background="#ccc"/> //设置背景颜色
4、ImageButton按钮控件:它是继承于ImageView类,相比Button控件它没有text属性
5、RadioButton控件:单选按钮,在RadioGroup下使用可设置多个RadioButton容器,一般RadioGroup下设置两个RadioButton。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
6、CheckBox控件:复选框按钮,可多选
7、ImageView控件:主要是显示图片,通过是src属性引用图片,
主要属性android:id设置id,获取控件都需要设置id值;android:src引用图片
<ImageView
android:id="@+id/iv_user_back_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/background"/>
8、ProgressBar控件:进度条
<ProgressBar
android:layout_width="60dp"
android:layout_height="60dp"/>