关于dp、px、sp以及线性布局、相对布局

来源:互联网 发布:校音器软件 编辑:程序博客网 时间:2024/05/22 03:12

1px (pixels)(像素):屏幕上的点 ,与密度相关。密度大了,单位面积上的px会比较多。 
2)dip或dp(与密度无关的像素)。这个和设备硬件有关,为了支持WVGA、HVGA和QVGA 5进制空间推荐使用这个。一种基于屏幕密度的抽象单位。设置一些view的宽高可以用这个,一般情况下,在不同分辨率,都不会有缩放的感觉。如果用px的话,320px占满HVGA的宽度,到WVGA上就只能占一半不到的屏幕了,那一定不是你想要的。 

3sp(与刻度无关的像素)放大像素– 主要处理字体的大小。

 

线性布局(LinearLayout):在该标签下的所有子元素会根据orientation属性的值来决定是按行或者是按列来逐个显示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:orientation="vertical"   

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent" >  

    

    <Button  

        android:id="@+id/button1"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/app_name" />  

   

    <Button  

        android:id="@+id/button2"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/hello_world" />  

   

    <Button  

        android:id="@+id/button3"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/test" />  

   

</LinearLayout>

 

相对布局则是根据控件的相对位置而言,比如居于按钮的左侧或者右侧

  <RelativeLayout   

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent">  

        <Button  

            android:id="@+id/button2"  

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:layout_toRightOf="@id/button1"  

            android:layout_alignTop="@id/button1"  

            android:text="@string/hello_world" />  

   

        <Button  

            android:id="@+id/button1"  

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:layout_alignParentLeft="true"  

            android:layout_alignParentTop="true"  

            android:text="@string/app_name" />  

   

    </RelativeLayout>

 

 

 

仅仅使用dp和sp不能完美适应屏幕,当换到不同尺寸的屏幕时还是会变形。