Android:屏幕自适应

demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8MTMzODgyOTQxN3w1NzAwOTV8MTczOTczphp

 

如下是Demo首页的预览图java

1、细说layout_weightandroid

    目前最为推荐的Android多屏幕自适应解决方案。布局

    该属性的做用是决定控件在其父布局中的显示权重,通常用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,通常横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。spa

    传统的layout_weight使用方法是将当前控件的layout_widthlayout_height都设置成fill_parent,这样就能够把控件的显示比例彻底交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的状况。不过对于2个控件还好,若是控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好肯定的。code

    因而就有了如今最为流行的0px设值法。看似让人难以理解的layout_height=0px的写法,结合layout_weight,却可使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。orm

    先看下面的stylesstyle_layout.xmlxml

复制代码

<?xml version="1.0" encoding="utf-8"?>
<resources>  
 
<!-- 全屏幕拉伸-->
  <style name="layout_full">  
    <item name="android:layout_width">fill_parent</item>  
    <item name="android:layout_height">fill_parent</item>  
  </style>
   
<!-- 固定自身大小-->
  <style name="layout_wrap">  
    <item name="android:layout_width">wrap_content</item>  
    <item name="android:layout_height">wrap_content</item>  
  </style>
 
<!-- 横向分布-->
  <style name="layout_horizontal" parent="layout_full">  
    <item name="android:layout_width">0px</item>  
  </style> 
    
<!-- 纵向分布-->
  <style name="layout_vertical" parent="layout_full">  
    <item name="android:layout_height">0px</item>  
  </style> 
         
</resources>

复制代码

能够看到,layout_widthlayout_height两个属性被我封装成了4style图片

    根据实际布局状况,选用当中的一种,不须要本身设置,看过我前一个ActivityGroupDemo的同窗应该很是熟悉了ip

    而后个人Demo的布局以下(weight_layout.xml

复制代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/layout_full"
        android:orientation="vertical">
        <LinearLayout 
                style="@style/layout_vertical"
                android:layout_weight="1"
                android:orientation="horizontal">
                 <View
                         style="@style/layout_horizontal"
                         android:background="#aa0000"
                         android:layout_weight="1"/>
                 <View
                         style="@style/layout_horizontal"
                         android:background="#00aa00"
                         android:layout_weight="4"/>
                 <View
                         style="@style/layout_horizontal"
                         android:background="#0000aa"
                         android:layout_weight="3"/>
                 <View
                         style="@style/layout_horizontal"
                         android:background="#aaaaaa"
                         android:layout_weight="2"/>                 
        </LinearLayout> 
        <LinearLayout 
                style="@style/layout_vertical"
                android:layout_weight="2"
                android:orientation="vertical">
                <View
                         style="@style/layout_vertical"
                         android:background="#ffffff"
                         android:layout_weight="4"/>        
                 <View
                         style="@style/layout_vertical"
                         android:background="#aa0000"
                         android:layout_weight="3"/>
                 <View
                         style="@style/layout_vertical"
                         android:background="#00aa00"
                         android:layout_weight="2"/>
                 <View
                         style="@style/layout_vertical"
                         android:background="#0000aa"
                         android:layout_weight="1"/>
 
        </LinearLayout>
</LinearLayout>

复制代码

整个界面布局看起来很是直观,只是嵌套的逻辑要本身理下。显示效果以下图,其中左面一个是480x800的界面,右面的是320x480的界面(后面的图也如此),能够看出显示比例和代码中彻底一致,我就很少说了,你们对照下就能看出来了。

 

2、自定义尺寸法

 不推荐已删除

3、java代码中设置宽高度

    也许不少人会反对这种方法,由于即便是官方也是推荐使用xml的方式写布局。不过咱们在这不会像Swing那样写那么多麻烦的布局代码,由于咱们只是在代码中从新设定控件的宽高度而已,其余属性依然是交给xml布局文件的。这个方法实际上是我跟同事偷学来的,虽然我不同意这样的方法,但他确确实实也是解决屏幕自适应问题的方案之一,并且它没我想象的那么复杂,其实很简单。

    首先咱们要作的是获取当前屏幕的宽高度,由于这个在后面要用到

    咱们能够写两个静态变量用来保存当前屏幕的宽高度:

public class Constant {
        public static int displayWidth;  //屏幕宽度        
        public static int displayHeight; //屏幕高度
        }

而后在第一个Activity启动的时候,获取这两个值

 

            DisplayMetrics displayMetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                Constant.displayWidth = displayMetrics.widthPixels;
                Constant.displayHeight = displayMetrics.heightPixels;

 

 布局代码咱们能够全都统一写成wrap-content,其实写成什么都无所谓,由于这个值只是暂时的 

复制代码

<?xml version="1.0" encoding="utf-8"?>
<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/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcccc"
    android:text="aaaaaaaa"/>
<Button  
        android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ccffcc"
    android:text="bbbbbbbbb"/>
<Button  
        android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ccccff"
    android:text="ccccccccc"/>
<Button  
        android:id="@+id/btn4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffcc"
    android:text="dddddddddddddddddd"/>   
</LinearLayout>

复制代码

最后咱们在ActivityonCreate方法里这么作 

复制代码

// 第一个按钮,宽度100%,高度10%
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                (int) (Constant.displayHeight * 0.1f + 0.5f));
                btn1.setLayoutParams(params);
                // 第二个按钮,宽度100%,高度30%
                LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                (int) (Constant.displayHeight * 0.3f + 0.5f));
                btn2.setLayoutParams(params2);
                // 第三个按钮,宽度50%,高度20%
                LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                                (int) (Constant.displayWidth * 0.5f + 0.5f),
                                (int) (Constant.displayHeight * 0.2f + 0.5f));
                btn3.setLayoutParams(params3);
                // 第三个按钮,宽度70%,高度填满剩下的空间
                LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
                                (int) (Constant.displayWidth * 0.7f + 0.5f),
                                LayoutParams.FILL_PARENT);
                btn4.setLayoutParams(params4);

复制代码

你们能够看到其实代码并不复杂,都能看得懂

    下面是效果显示图

多布局

    作为最后的方法,也是最后一个才会考虑的方法,那就是为不一样的尺寸界面单独写布局。不到万不得已不要用这个方法,相信很多人和我同样都被逼着用过这个方法吧。须要说明的是,横竖屏切换使用不一样布局也是用这个方法解决的;代码我就不上了,给你们看两张图吧,一个是1个布局的,一个是写了多布局的,你们一看就明白了

补充一下,写多个布局的时候,配置文件必定要加上这段配置代码,否则有时可能会出问题
   

 <supports-screens android:largeScreens="true"
                android:normalScreens="true" android:anyDensity="true" />



<ignore_js_op>4-2.png

5、其余

    以上说的都是多个屏幕显示相同内容须要考虑的问题,还有一种是在不一样的屏幕上显示内容不一样的状况,其实这个问题咱们每每是用滚动视图来解决的,也就是ScrowView;须要注意的是ScrowView中使用layout_weight是无效的,既然使用ScrowView了,就把它里面的控件的大小都设成固定的吧。

    此外关于图片的自适应问题,主要是2点,一个是9patch图,这个东西你们都要学会去作,不难;不过有些编译器在识别9patch图时会出这样那样的bug,像个人Eclipse就不认这个,而同一个9patch图在别的电脑上倒是没问题的,

    第二个要说的是我曾经被困扰的一个问题,对于480x800  480x854这两个尺寸,他们显示同一个图片时,总有一个会拉伸(若是9patch能够解决的还好)。其实当初困扰个人是,这两个尺寸都是hdpi的,觉得没法给这两个屏幕作不一样的图片。后来无心中发现,图片能够和布局同样分多个尺寸的,而不只仅是根据密度分,也就是说你能够写这样的文件夹drawable-hdpi-800x480drawable-hdpi-854x480,在它们里面放不一样的图片,这样图片也能自适应了。

相关文章
相关标签/搜索