zoukankan      html  css  js  c++  java
  • Andriod 学习笔记 layout布局

    一:不同的layout
      Android手机屏幕大小不一,有480x320, 640x360,
      800x480.怎样才能让App自动适应不同的屏幕呢?
      
      其实很简单,只需要在res目录下创建不同的layout文件夹,比如layout-640x360,layout-800x480,所有的layout文件在编译之后都会写入R.java里,而系统会根据屏幕的大小自己选择合适的layout进行使用。


    二:hdpi、mdpi、ldpi

      在之前的版本中,只有一个drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三个,这三个主要是为了支持多分辨率。

      drawable- hdpi、drawable- mdpi、drawable-ldpi的区别:

      (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)
      (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)
      (3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)

      系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。
      更正:应该是对应不同density 的图片  
     

      在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。

    [i]备注:三者的解析度不一样,就像你把电脑的分辨率调低,图片会变大一样,反之分辨率高,图片缩小。[/i]
    屏幕方向:

    横屏竖屏自动切换: 

    可以在res目录下建立layout-port-800x600和layout-land两个目录,里面分别放置竖屏和横屏两种布局文件,这样在手机屏幕方向变化的时候系统会自动调用相应的布局文件,避免一种布局文件无法满足两种屏幕显示的问题。

    不同分辨率横屏竖屏自动切换:
    以800x600为例
    可以在res目录下建立layout-port-800x600和layout-land-800x600两个目录
    不切换:

    以下步骤是网上流传的,不过我自己之前是通过图形化界面实现这个配置,算是殊途同归,有空我会把图片贴上来。

    还要说明一点:每个activity都有这个属性screenOrientation,每个activity都需要设置,可以设置为竖屏(portrait),也可以设置为无重力感应(nosensor)。
    要让程序界面保持一个方向,不随手机方向转动而变化的处理办法: 
    在AndroidManifest.xml里面配置一下就可以了。加入这一行android:screenOrientation="landscape"。
    例如(landscape是横向,portrait是纵向):
    Java代码:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android=http://schemas.android.com/apk/res/android package="com.ray.linkit" android:versionCode="1" android:versionName="1.0"> 
       <application android:icon="@drawable/icon" android:label="@string/app_name">

       <activity android:name=".Main" android:label="@string/app_name" android:screenOrientation="portrait">
                <intent-filter> 
                    <action android:name="android.intent.action.MAIN" /> 
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter> 
       </activity> 
       <activity android:name=".GamePlay"  android:screenOrientation="portrait"></activity> 
     <activity android:name=".OptionView"  android:screenOrientation="portrait"></activity>
    </application>
        <uses-sdk android:minSdkVersion="3" />
    </manifest>

    另外,android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!

    有的程序适合从竖屏切换到横屏,或者反过来,这个时候怎么办呢?可以在配置Activity的地方进行如下的配置android:screenOrientation="portrait"。这样就可以保证是竖屏总是竖屏了,或者landscape横向。

    而有的程序是适合横竖屏切换的。如何处理呢?首先要在配置Activity的时候进行如下的配
    置:android:configChanges="keyboardHidden|orientation",另外需要重写Activity的
    onConfigurationChanged方法。实现方式如下,不需要做太多的内容:

    @Override 
    public void onConfigurationChanged(Configuration newConfig) {       
      super.onConfigurationChanged(newConfig); 
          if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
                // land do nothing is ok 
           } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 

        // port do nothing is ok 

       }
    }


    写一个支持多分辨的程序,基于1.6开发的,建立了三个资源文件夹drawable-hdpi drawable-mdpi
    drawable-ldpi,里面分别存放72*72 48*48
    36*36的icon图标文件。当我在G1(1.5的系统)上测试时,图标应该自适应为48*48才对啊,但实际显示的是36*36。怎么才能让其自适应
    48*48的icon图标呢

    解决办法 drawable-hdpi drawable-mdpi
    drawable-ldpi改成drawable-480X320
    drawable-800X480的多分辨支持的文件夹


    实现的方法:
    layout_weight属性的使用,
    合适的背景,一般是要足够大
    合理的布局
    在AndroidManifest.xml
    设置
        <uses-sdk android:minSdkVersion="7“


    系统的计算方式:


    File DisplayHardware.cpp

    void DisplayHardware::init(uint32_t dpy){

      ….

        /* Read density from build-specific ro.sf.lcd_density property
         * except if it is overridden by qemu.sf.lcd_density.
         */
        if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
        if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
                  LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
                  strcpy(property, "160");       
        }
        } else {
            /* for the emulator case, reset the dpi values too */
            mDpiX = mDpiY = atoi(property);    
        }

        mDensity = atoi(property) * (1.0f/160.0f);
      WindowManagerService.java
      boolean computeNewConfigurationLocked(Configuration config) {
        ……
            if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
                // Note we only do this once because at this point we don't
                // expect the screen to change in this way at runtime, and want
                // to avoid all of this computation for every config change.

                int longSize = dw;
                int shortSize = dh;
                if (longSize < shortSize) {
                    int tmp = longSize;
                    longSize = shortSize;
           shortSize = tmp;
                }

                longSize = (int)(longSize/dm.density);
                shortSize = (int)(shortSize/dm.density);

                // These semi-magic numbers define our compatibility modes for
                // applications with different screens.  Don't change unless you
                // make sure to test lots and lots of apps!
                if (longSize < 470) {
                  // This is shorter than an HVGA normal density screen   (which
                    // is 480 pixels on its long side).
                    mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL  | Configuration.SCREENLAYOUT_LONG_NO;
                } else {
                    // Is this a large screen?
                    if (longSize > 640 && shortSize >= 480) {
            // VGA or larger screens at medium density are the point
                          // at which we consider it to be a large screen.
                         mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
                    } else {
                        mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
                        // If this screen is wider than normal HVGA, or taller
           // than FWVGA, then for old apps we want to run in size
                        // compatibility mode.

                        if (shortSize > 321 || longSize > 570)
           {
                            mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;                   
             }
                    }
                    // Is this a long screen?
                    if (((longSize*3)/5) >= (shortSize-1)) {
                        // Anything wider than WVGA (5:3) is considering to be long.
                        mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
                    } else {
                        mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
                    }}
            }

            config.screenLayout = mScreenLayout;

            config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
            config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
           
        mPolicy.adjustConfigurationLw(config);

            return true;
        }

        转载自:http://hi.baidu.com/sdfiyon/item/9221282b9d72defb51fd8733

  • 相关阅读:
    React初识整理(二)--生命周期的方法
    React初识整理(一)
    前后端分离浅析
    url地址数据参数转化JSON对象(js三种方法实现)
    js和JQuery中的获取宽、高、位置等方法整理
    游民轮播图效果实现
    焦点轮播图效果实现
    iOS开发debug集锦
    实现一个 RESTful API 服务器
    从iOS的图片圆角想到渲染
  • 原文地址:https://www.cnblogs.com/loongsoft/p/2872351.html
Copyright © 2011-2022 走看看