android 多屏幕适配 : 第一部分

1、在xml布局文件中,控件的宽度和高度用  dp ;   字体大小用 sp

2、根据屏幕的宽高来动态的适配 , 获取屏幕的宽高的两种方法:

    第一种方法:

    /**
     * 屏幕的宽度
     * 屏幕的高度
     * @return
     */
    public void initPhone1( Activity activity ){
        int phone_Width  = activity.getWindowManager().getDefaultDisplay().getWidth() ;    //单位是 px
        int phone_Height = activity.getWindowManager().getDefaultDisplay().getHeight() ;   //单位是 px
    }

    这种方法会报警告,The method getWidth() from the type Display is deprecated

    意思是这种方法已经过时,所以建议用第二种方法:

   

第二种方法:

  

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int  Phone_width  = dm.widthPixels ;
int Phone_height = dm.heightPixels ;

注意:1、在 Java 代码中获取的宽度和高度,以 px (像素) 为单位。 与xml 文件中的 dp 不一样 。

         2、经过测试,用两种方法分别获取手机屏幕的宽度和高度,得到的结果是一样的 。

              我的手机是小米1 ,480 x 854  px

3、通常情况下,一个 layout 布局文件里面的控件的大小,有两种设置控件宽高的 方法 。

    一种是在 xml 中设置 , 如果一个控件在 xml 中有定义控件的宽度和高度用  dp ;   字体大小用 sp 。

    另外一种 就是在java 代码中动态设置 。

   

    TextView tv2 = (TextView) findViewById( R.id.tv2 ) ;
    LinearLayout.LayoutParams params2 = (LayoutParams) tv2.getLayoutParams() ;
    params2.width = 300 ;    //这里的300代表  300 px 
    params2.height = 100 ;   //这里的100代表  100 px 
    tv2.setLayoutParams( params2 );


4、因为在 xml 布局中 单位是 dp , 在 java 代码中 的单位是 px 。

    为了两者的大小保持一致,所以需要将两者进行换算 。

  DensityUtil 类

package com.example.bb;

import android.content.Context;

public class DensityUtil {
    /** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */  
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  

    /** 
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */  
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  

}

5、由于android 的屏幕大小有很多中,分辨率也是多种多样的 。

   为了准确的获取屏幕的高度和宽度,需要在AndroidManifest.xml 中加入 supports-screens 节点 。

   

?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app01"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <!-- 获得手机正确的宽度和高度 -->
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.app01.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

     

  

  

   

原文地址:https://www.cnblogs.com/zhaoyanjun/p/4547185.html