长度单位与内外边距

1. 距离单位之px

2. 距离单位之dp

3. 距离单位之sp

4. 控件的外边距和内边距

 

1. 距离单位之px

    px 即像素, 每个像素可以显示红绿蓝

    每个格子就是一个像素

    分辨率代表可以显示对少个点, 800 * 480

   

       如果定义一个像素为20宽度的, 就会占领屏幕宽度的一半. 

2. dpi

       屏幕4.3英寸, 指的是对角线是4.3英寸. 两个4.3英寸屏幕但是分辨率有可能不同

      

        dpi (dots per inch)  =  (800像素的平方 + 480像素的平方) 的平方根  /  4.3英寸

        dpi表示屏幕的细腻程度

3. 距离单位之dp

        dpi 会给程序开发带来麻烦

       

        px (device independent pixels) = dp * ( dpi / 160)

        若下图屏幕尺寸一样大, 则左侧的 dpi 是右侧的一半

        若设置宽度为 160 dp

       

         dpi=160    160px = 160dp*(160/160)

         dpi=320    320px = 160dp*(320/160)

         因此使用dp可以适合各种屏幕分辨率!!!

3. 距离单位之sp

         sp : scaled pixels  可缩放的像素

         sp通常用于指定字体的大小

         当用户修改手机显示字体时, sp会随之改变

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="first.pack.MainActivity$PlaceholderFragment" >
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:textSize="50dp"
16         android:background="#FF0000"
17         android:text="@string/hello_world" />
18 
19     <TextView
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:textSize="50sp"
23          android:background="#00FF00"
24         android:text="@string/hello_world" />
25 </LinearLayout>

         运行代码之后发现

        

         在虚拟设备 setting --> Display  ----> Font size ---> Huge

        

         显示字体跟随手机设置一起变化.

         因此, 在设置字体大小时, 通常使用sp作为单位, 其他的多是使用dp作为单位

4. 控件的外边距和内边距

           

          

  

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="first.pack.MainActivity$PlaceholderFragment" >
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:background="#FF0000"
16         android:layout_margin="30dp"     //外边距
17         android:text="@string/hello_world" />
18     
19     <TextView
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:background="#00FF00"
23         android:layout_marginTop="30dp"
24         android:text="@string/hello_world" />
25 
26 </LinearLayout>

 

        

          

<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:orientation="vertical"
    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="first.pack.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:layout_margin="30dp"
        android:text="@string/hello_world" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00FF00"
        android:padding="20dp"     //内边距
        android:layout_marginTop="30dp"
        android:text="@string/hello_world" />

</LinearLayout>

 

          

posted @ 2014-06-05 10:51  Mirrorhanman  阅读(244)  评论(0编辑  收藏  举报