android UI-简单布局

来源:互联网 发布:linux中链接目的 编辑:程序博客网 时间:2024/05/22 05:14

一、视图的View的基本属性

android的所有控件和布局都是view类直接或间接派生而来的。

(1)xml布局中常用的属性定义:

id 该视图的编号

layout_width 视图的宽度

layout_height 视图的高度

layout_margin 该视图和周围视图之间的空白距离(包括上、下、左、右)

layout_marginTop 单独指定视图与上边视图的距离

layout_marginBottom 单独指定视图与底部视图的距离

layout_marginLeft 单独指定视图与左边视图的距离

layout_marginRight 单独指定视图与右边视图的距离

minWidth 该视图的最小宽度

minHeight 该视图的最小高度

background 指定该视图的背景,背景可以是颜色

layout_gravity 该视图与上级视图的对齐方式、对齐方式的取值可以用“|”进行多种拼接

padding 该视图边缘与内部内容之间的空白距离

paddingTop 该视图边缘与内部内容上边的空白距离

paddingBottom 该视图边缘与内部内容底部的空白距离

paddingLeft 该视图边缘与内部内容左边的空白距离

paddingRight 该视图边缘与内部内容右边的空白距离

visibility 该视图的可视类型(visible可见默认,invisible不可见,但还占着位置,gone消失看不到而且看不到位置)

(2)代码中常用的属性设置

setLayoutParams 设置该视图的布局参数。参数对象的构造函数可以设置视图的宽度和高度。其中,layoutParams.MATCH_PARENT表示与上级视图一样宽,也可以是layoutParams.WRAP_CONTENT,表示与内部内容一样宽;参数对象的setMargins方法可以设置该视图与周围视图之间的空白距离。

setMinimumWidth 设置该视图的最小宽度

setMinimumHeight 设置该视图的最小高度

setBackgroundColor 设置该视图的背景颜色

setBackgroundDrawable 设置该视图的背景图片

setBackgroundResource 设置该视图的背景资源id

setPadding 设置该视图边缘与内部内容之间的空白距离

setVisibility 设置该视图的可视类型

例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="300dp"android:background="#00aaff"android:orientation="vertical"android:padding="5dp" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="20dp"android:background="#ffff99"android:padding="60dp" ><Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#ff0000" /></LinearLayout></LinearLayout>

二、线性布局LinearLayout

(1)xml中linearLayout的特有属性

orientation 有水平方向和垂直方向

gravity 布局的内部视图与该布局的对齐方式

layout_weight 权重

例:layout_gravity和gravity演示界面


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="300dp"android:background="#ffff99"android:orientation="horizontal"android:padding="5dp" ><LinearLayoutandroid:layout_width="0dp"android:layout_height="200dp"android:layout_weight="1"android:layout_gravity="bottom"android:gravity="left"android:background="#ff0000"android:layout_margin="10dp"android:padding="10dp"android:orientation="vertical"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="#00ffff" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="200dp"android:layout_weight="1"android:layout_gravity="top"android:gravity="right"android:background="#ff0000"android:layout_margin="10dp"android:padding="10dp"android:orientation="vertical"><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="#00ffff" /></LinearLayout></LinearLayout>

三、滚动视图ScrollView

滚动视图也分垂直和水平方向。

垂直滚动时,layout_width要设置为match_parent,layout_height要设置为wrap_content

水平滚动时,layout_width要设置为wrap_content,layout_height要设置为match_parent

注意:滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child.

例:滚动视图


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><HorizontalScrollViewandroid:layout_width="wrap_content"android:layout_height="200dp"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="horizontal"><Viewandroid:layout_width="400dp"android:layout_height="match_parent"android:background="#aaffff" /><Viewandroid:layout_width="400dp"android:layout_height="match_parent"android:background="#ffff00" /></LinearLayout></HorizontalScrollView><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="400dp"android:background="#00ff00" /><Viewandroid:layout_width="match_parent"android:layout_height="400dp"android:background="#ffffaa" /></LinearLayout></ScrollView></LinearLayout>

如果ScrollView的内容不能充满屏幕,则

android:layout_height="match_parent"android:fillViewport="true"




原创粉丝点击