px像素 2560*1440 320*240
dp 一英寸上存在160px则1dp=1px(不同手机1dp像素不同但相对来说1dp在不同手机上比例相同)
sp 文本的大小跟dp一样
margenLeft:Button左边离开15dp margintop margenbottom margen(所有)
1、linearlayout布局,gravity可以定义下面Button的位置(现在是横向布局 左边),如果有两个就一起排
这是在右下角(Button没定义margen的情况下)
当布局没写gravity,Button里写layout_gravity就跟match一样在父空间的中间(效果没区别只是写法不同,能单独放置)
两个Button分占屏幕,用weight定义所占比例
<LinearLayout 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:weightSum="5" //相当于把屏幕分成了5份 android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:layout_width="wrap_content" 如果写成android:layout_width="0dp"就把Button本身所占的空间设置成了0 android:layout_weight="3"//占3份 /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2"占两份 /> </LinearLayout> 练习题:linearlayout嵌套生成如下模式 解析:首先是屏幕3分割,linearlayout、button、linearlayout 其次两个linearlayout是水平的分成左右两部分button和垂直的linearlayout(注意垂直和水平的参数) 垂直的linearlayout又有button 代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="3" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" /> <LinearLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="2" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp" /> <Button android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp" /> </LinearLayout> </LinearLayout> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout
2、 RelativeLayout布局
左下角对齐alignParentLeft Right Bottom top,组合可以实现四个角对齐
<Button android:layout_alignParentLeft="true" //父类空间左对齐 android:layout_alignParentBottom="true" //父类空间下对齐 android:layout_width="wrap_content" android:layout_height="wrap_content" /> 中心对齐centerHorizontal和centerVertical,centerInparent与这两个的组合效果类似
<Button android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
相对于控件对齐toLeftof、torightof、above、below
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/button" //android:layout_toRightOf="@id/button" //中间Button的id是button //android:layout_above="@id/button" //android:layout_below="@id/button" /> 两者可以组合,形成右上方对齐、左上方对齐、左下、右下
3、TableLayout是制表布局,必须先写Tablerow
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <Button /> <Button /> </TableRow> </TableLayout>
4、FrameLayout是帧布局,可以两个控件套在一起,可以做页面切换
只有一个visibility属性,可设置为visible可见,invisible不可见但是占空间,gone不占空间不可见
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" /> </FrameLayout>
5、AbsoluteLayout是绝对布局,可以任意放置控件,控件位置是dp规定,但是由于屏幕大小差距,导致应用不适配