【0022】Android 基础7-android中常用布局
【课程代号】黑马安卓74期\02_Android基础(day14-day25)\day02\video\3.android中常用布局.avi
常用的是5大布局,常用的是有2~3个;
【1】线性布局-垂直线性和水平线性
其实,这两种布局使用的都是一种布局;


或者选择:other


【1.1】【区别】 Android 布局 fill_parent、wrap_content和match_parent的区别和作用
这三个东东都是用来设置你的控件在布局中的大小。其实具体的意思知道懂点英语就很简单了。
wrap_content
wrap 翻译过来是包裹,conten是内容。那么这个就是包裹内容的意思,也就是说你的控件里面的内容有多大,这个控件就有多大。
fill_parent和match_parent
关于这个两个东西网站争议还是比较大的,有人说他俩一个意思,2.2版本后更新。为了兼容低版本建议使用fill_parent。
我们还是从字面意思翻译一下。
fill一般翻译过来是填充,充满的意思
match一般翻译过来是相同,适应的意思。
经过我本人的亲测,使用这个两个属性作用同一个控件,确实效果是一致的,他们的意思是
让作用的控件填充满父容器的其他空间。有点类似C#布局中的Dock属性。
最后还有一点是非常难理解的,就是这个其他空间怎么来理解,其实也很简单,上几个图你就知道了。
使用线性布局LinearLayout垂直摆放两个按钮,
如果上面按钮的高度是fill_parent或者match_parent,那么下面的按钮根本显示不出来。
反之,上面是wrap_content,下面是fill_parent或者match_parent,那么上面会有,下面的这个按钮会填充满剩余的其他部分。
【1.2】居中对齐的方法-gravity和layout_gravity

【方法1】gravity

【方法2】layout_gravity

【1.3】Margin属性-上下左右需要分开定义



【1.4】padding属性



【2】RelativeLayout :相对布局
相对布局:不是从上到下,也不是从左到右;都是基于左上角的位置进行布局的;



【3】帧布局:一层一层叠加的布局

下面网页版的视频播放界面就是属于帧布局:最下面一层是视频,上面一层是弹幕,在上面一层是按钮,再上面一层是广告;



1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:background="#ffff00" > 10 </LinearLayout> 11 12 <LinearLayout 13 android:layout_margin="50dp" 14 android:layout_width="fill_parent" 15 android:layout_height="fill_parent" 16 android:background="#ff00ff" > 17 </LinearLayout> 18 19 <ImageView 20 android:layout_gravity="center" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:src="@drawable/ic_launcher" 24 25 /> 26 27 </FrameLayout>
【4】表格布局


1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <!-- TableLayout 中一个TableRow代表一行,TableRow中的一个控件代表一列 --> 7 8 <TableRow 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="第一行第一列" /> 16 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="第一行第二列" /> 21 </TableRow> 22 23 24 25 <TableRow 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" > 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="第二行第一列" /> 33 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="第二行第二列" /> 38 </TableRow> 39 40 </TableLayout>
【5】绝对布局-被抛弃了
因为在每一个控件中都存在一个坐标的位置,在做屏幕适配的时候很麻烦;

浙公网安备 33010602011771号