当前位置:操作系统 > 安卓/Android >>

Android Developers:支持不同的屏幕大小

这节课程向你展示了通过如下方式支持不同的屏幕大小: 
确保你的布局能适当地调整大小来适应屏幕 
根据屏幕的配置提供适当的UI布局 
确保正确的布局被应用到正确的屏幕 
提供正确缩放的位图 
 
使用"wrap_content"和“match_parent" 
——————————————————————————————————————————————————————————————
为了确保你的布局是灵活的,并且适应不同的屏幕大小,你应该使用”wrap_content"和“match_parent"设置一些视图组件的宽度和高度。如果你使用"wrap_content",这个视图的宽度或者高度设置为符合视图内部内容需要的最小尺寸。然而”match_parent"(在API8之前又名"fill_parent")使组件扩展来匹配他的父视图的尺寸。 
 
通过使用“wrap_content"和"match_parent"尺寸值替代固定值尺寸,你的视图要么仅仅使用视图必要的空间,或者扩展来填充可用的空间。例如: 
[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:orientation="vertical"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent">   
    <LinearLayout android:layout_width="match_parent"    
                  android:id="@+id/linearLayout1"     
                  android:gravity="center"   
                  android:layout_height="50dp">   
        <ImageView android:id="@+id/imageView1"    
                   android:layout_height="wrap_content"   
                   android:layout_width="wrap_content"   
                   android:src="@drawable/logo"   
                   android:paddingRight="30dp"   
                   android:layout_gravity="left"   
                   android:layout_weight="0" />   
        <View android:layout_height="wrap_content"    
              android:id="@+id/view1"   
              android:layout_width="wrap_content"   
              android:layout_weight="1" />   
        <Button android:id="@+id/categorybutton"   
                android:background="@drawable/button_bg"   
                android:layout_height="match_parent"   
                android:layout_weight="0"   
                android:layout_width="120dp"   
                style="@style/CategoryButtonStyle"/>   
    </LinearLayout>   
   
    <fragment android:id="@+id/headlines"    
              android:layout_height="fill_parent"   
              android:name="com.example.android.newsreader.HeadlinesFragment"   
              android:layout_width="match_parent" />   
</LinearLayout>   
注意,实例使用”wrap_content"和“match_parent"指定组件的尺寸,而不是指定特定的大小。这允许布局正确的适配不同的屏幕大小和方向。 
 
例如,这是这个布局在竖屏和横屏模式下的养子。注意组件的大小自动适配宽度和高度。 
 
使用RelativeLayout 
——————————————————————————————————————————————————————————————
你可以使用嵌入LinearLayout实例和"wrap_content"和”match_parent"尺寸结合,构建相当复杂的布局。然而,LinearLayout不允许是精确的控制孩子视图的位置关系;在LinearLayout中的视图简单一行一行排列。如果你需要子视图有变化的朝向而不是一条直线,一个更好的解决办法是经常使用RelativeLayout,它允许你指定你的布局组件之间的空间关系。例如,你能对齐一个子视图在屏幕的左边,并且另一个视图在屏幕的右边。 
 
例如: 
[html] 
<?xml version="1.0" encoding="utf-8"?>   
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent">   
    <TextView   
        android:id="@+id/label"   
        android:layout_width="match_parent"   
        android:layout_height="wrap_content"   
        android:text="Type here:"/>   
    <EditText   
        android:id="@+id/entry"   
        android:layout_width="match_parent"   
        android:layout_height="wrap_content"   
        android:layout_below="@id/label"/>   
    <Button   
        android:id="@+id/ok"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_below="@id/entry"   
        android:layout_alignParentRight="true"   
        android:layout_marginLeft=&
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,