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

Android官方提供的支持不同屏幕大小的全部方法

本文将告诉你如何让你的应用程序支持各种不同屏幕大小,主要通过以下几种办法:


让你的布局能充分的自适应屏幕
根据屏幕的配置来加载合适的UI布局
确保正确的布局应用在正确的设备屏幕上
提供可以根据屏幕大小自动伸缩的图片
使用 "wrap_content" 和 "match_parent"
 为了确保你的布局能够自适应各种不同屏幕大小,你应该在布局的视图中使用"wrap_content"和"match_parent"来确定它的宽和高。如果你使用了"wrap_content",相应视图的宽和高就会被设定成刚好能够包含视图中内容的最小值。而如果你使用了"match_parent"(在Android API 8之前叫作"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> 

<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=&q

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,