您的位置:首页 > 移动开发 > Android开发

android学习笔记(2.1)--7.20--(5中布局方式+android单位)

2014-07-20 17:03 591 查看
  

五大布局Layout:

 LinearLayout 
线性布局

 RelativeLayout 相对布局

 AbsoluteLayout 绝对布局

FrameLayout  帧布局

 
TableLayout 表格布局

  

1.LinearLayout线性布局:

LinearLayout 线性布局
android:orientation="horizontal"
 制定线性布局的排列方式    

水平 horizontal    

垂直 vertical

android:gravity 控制当前控件内容显示区域

android:gravity意思是这个控件自己的“重力”,在通俗点就是控件上面的东西的位置(图片,文本等)

举个例子:一个TextView里面的文本默认居左作对齐的,你想让这些文本居中的话,只要在这个TextView的属性里加上android:gravity="center"

所有的参数分别是center(居中),bottom(下),top(上),right(右),left(左)

其他的一些是不常用的上面这些足够用了,比如要弄一个左下的android:gravity=" left| bottom "就OK了

layout_gravity 当前控件在父元素的位置

Layout_weightSum

Layout_weight 额外空间分配(权重)

首先计算出额外空间(可以为负)如果额外空间不为0并且有子View的layout_weight不为0的话按layout_weight分配额外空间:在控件大小安原有的大小设置完成后,

如果还有额外空间,那么可以用来分配给设置Layout_weight的控件。
<?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" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
<strong><span style="color:#ff0000;">  android:layout_weight="1"</span></strong>
android:text="第二个"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"/>
</LinearLayout>


Layout_weightSum不指定时安子控件的Layout_weight相加决定

android:visibility="invisible"控制布局是否显示

显示 visible

不显示,但占空间 invisible

隐藏 gone

2. 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 在父布局中居中

3. AbsoluteLayout  绝对布局:

android:layout_x指定控件在父布局的x轴坐标

android:layout_y指定控件在父布局的y轴坐标

p.s 原点在左上角

4. FrameLayout     帧布局:

帧布局每次添加的控件都显示在最上面,最后显示在界面上的是最后添加的一个控件,可以理解为Z轴值不同,后加的在上面。

5. TableLayout     表格布局:

android:shrinkColumns 收缩列

android:stretchColumns 拉伸列

android:collapseColumns 隐藏列

android:layout_column 指定列(作用在列的身上)

android:layout_span 合并列(作用在列的身上)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<span style="color:#ff0000;">android:shrinkColumns="1"</span>>

<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0行第0列"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0行第1列"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0行第2列"/>

</TableRow>
<TableRow >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1行第1列"
<strong><span style="color:#ff0000;"> android:layout_column="1"
android:layout_span="2"</span></strong>
/>
</TableRow>

</TableLayout>


p.s:TableRow单元行里的单元格的宽度小于默认的宽度时就不起作用,其默认是fill_parent,高度可以自定义大小。

android单位:

px (pixels)像素: 

     一般HVGA代表320x480像素,这个用的比较多。

dip或dp (device independent pixels)设备独立像素:

    这个和设备硬件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。(使用dp可以理解为320*480的px,会自动适配)

sp (scaled pixels — best for text size)比例像素:

    主要处理字体的大小,可以根据用户系统的字体自适应。

除了上面三个显示单位,下面还有几个不太常用:

 in (inches)英寸

 mm (millimeters)毫米  

 pt (points)点,1/72英寸

为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。

P.S:

常见的密度比值:
240*320 的密度比值是: 0.75
320*480 的密度比值是: 1.0
480*800 的密度比值是: 1.5

float density = getResources().getDisplayMetrics().density;

1.0 * 160dp = 160px

0.75 * 160dp = 120px

1.5 * 160dp = 240px
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 个人笔记