【Android基础】常用的布局和单位的简介
16lz
2021-01-26
五大布局Layout
LinearLayout 线性布局
下面列出的部分常用的属性
android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 verticalgravity 控制当前控件内容显示区域layout_gravity 当前控件在父元素的位置Layout_weightSumLayout_weight 额外空间分配(权重)android:visibility="invisible" 控制布局是否显示 显示 visible 不显示,但占空间 invisible 隐藏 gone
RelativeLayout 相对布局
android:layout_toRightOf 在指定控件的右边android:layout_toLeftOf 在指定控件的左边android:layout_above 在指定控件的上边android:layout_below 在指定控件的下边android:layout_alignBaseline 跟指定控件水平对齐android:layout_alignLeft 跟指定控件左对齐android:layout_alignRight 跟指定控件右对齐android:layout_alignTop 跟指定控件顶部对齐android:layout_alignBottom 跟指定控件底部对齐android:layout_alignParentLeft 是否跟父布局左对齐android:layout_alignParentTop 是否跟父布局顶部对齐android:layout_alignParentRight 是否跟父布局右对齐android:layout_alignParentBottom 是否跟父布局底部对齐android:layout_centerVertical 在父布局中垂直居中android:layout_centerHorizontal 在父布局中水平居中android:layout_centerInParent 在父布局中居中
AbsoluteLayout 绝对布局
AbsoluteLayoutandroid:layout_x 指定控件在父布局的x轴坐标android:layout_y 指定控件在父布局的y轴坐标
TableLayout 表格布局
android:shrinkColumns 收缩列android:stretchColumns 拉伸列android:collapseColumns 隐藏列android:layout_column 指定列(作用在列的身上)android:layout_span 合并列(作用在列的身上)
TableRow单元行里的单元格的宽度小于默认的宽度时就不起作用,其默认是fill_parent,高度可以自定义大小
FrameLayout 帧布局
帧布局每次添加的控件都显示在最上面,最后显示在界面上的是最后添加的一个控件
单位的概念
- px (pixels)像素,一般HVGA代表320x480像素,这个用的比较多。
- dip或dp (device independent pixels)设备独立像素,这个和设备硬 件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
- sp (scaled pixels — best for text size)比例像素,主要处理字体的大小,可以根据用户系统的字体自适应。
- 除了上面三个显示单位,下面还有几个不太常用:
in (inches)英寸 mm (millimeters)毫米 pt (points)点,1/72英寸
总结:为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。
例子
这里先给出帧布局和线性布局的例子,之后会给出相对布局的例子。
FrameLayout例子
<FrameLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.yzx.usualLayout.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000"/> <TextView android:id="@+id/textView1" android:layout_width="150dip" android:layout_height="150dip" android:layout_gravity="center" android:background="@android:color/black" /> <TextView android:id="@+id/textView2" android:layout_width="100dip" android:layout_height="100dip" android:layout_gravity="center" android:background="@android:color/white" /></FrameLayout>
运行结果:
LinearLayout例子
<?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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="205dip" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/black" /> <TextView android:id="@+id/textView2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/darker_gray" /> <TextView android:id="@+id/textView3" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/white" /> <TextView android:id="@+id/textView4" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="#FF0000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="205dip" android:orientation="vertical" > <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@android:color/darker_gray" /> <TextView android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@android:color/black" /> <TextView android:id="@+id/textView7" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@android:color/white" /> <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#FF0000" /> </LinearLayout></LinearLayout>
运行结果:
更多相关文章
- Android(安卓)调用系统相机返回data为null
- Android学习笔记16:Button控件图文混排效果的实现
- 安卓基础知识
- 判断Edittext是否为几种空方法
- Android关于滚动View标题栏渐变的解决方案
- 布局demo二:shape的使用
- Android(安卓)自定义View实现任意布局的RadioGroup
- android动画详解
- android之animation(一)