Android - 处理不同的屏幕尺寸

时间:2013-07-02 09:27:04

标签: android

我怀疑这是一个处理得很好的主题,因为我已经阅读了如下所示的几个帖子,但我似乎遇到了屏幕尺寸问题。

例如,对于使用我在下面的代码中计算的华硕FonePad,我计算屏幕尺寸为6.833英寸,但在进一步研究该设备时,它被宣传为7英寸设备。这导致我拿起“错误的”屏幕尺寸。

下面是代码,说明我如何使用我的应用程序计算设备的屏幕尺寸,然后调用正确的布局:

   // Splash screen delay
public final void SplashScreenThread() {

    //  For tracking purposes record in logcat SplashScreenThread() method has been called
    Log.i(TAG + "onCreate()", "SplashScreenThread(); HAS STARTED");

    // Thread for the display of the SplashScreen
    SplashThread = new Thread() {
        @Override
        public void run() {

            try {
                int waited = 0;
                while(_active && (waited < SplashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {

                // do nothing
                Log.e(TAG + "SplashScreenThread()", "SplashScreenThread() ERROR IS AS FOLLOWS: " + e.toString());
            } finally {
                    // Call method finish to kill any 
                    finish();

                    // Fetch the screen size in order to call correct layout wrt the next screen - firstly create and object of type DisplayMetrics
                    DisplayMetrics DeviceScreenSize = new DisplayMetrics();

                    // Secondly fetch the window manager using method chaining
                    getWindowManager().getDefaultDisplay().getMetrics(DeviceScreenSize);

                    // Calculate the width of the screen in inches
                    double x_DeviceScreenSize = Math.pow(DeviceScreenSize.widthPixels/DeviceScreenSize.xdpi,2);

                    // Record in logcat DeviceScreenSize.xdpi
                    Log.d(TAG,"DeviceScreenSize.widthPixels is: " + DeviceScreenSize.widthPixels + " and DeviceScreenSize.xdpi is: " + DeviceScreenSize.xdpi);
                    Log.d(TAG,"DeviceScreenSize.heightPixels is: " + DeviceScreenSize.heightPixels + " and DeviceScreenSize.ydpi is: " + DeviceScreenSize.ydpi);

                    // Calculate the height of the screen in inches
                    double y_DeviceScreenSize = Math.pow(DeviceScreenSize.heightPixels/DeviceScreenSize.ydpi,2);

                    // Obtain screen size using pythagoras theorem
                    Device_Screen_Size_In_Inches_To_Display_Log_In_Screen = Math.sqrt(x_DeviceScreenSize + y_DeviceScreenSize);

                    // Record in logcat screen size in inches
                    Log.e(TAG,"DeviceScreenSize.widthPixels is: " + DeviceScreenSize.widthPixels + "\nDeviceScreenSize.heightPixels is: " + DeviceScreenSize.heightPixels + "\nScreen inches : " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);

                    // Identify device screen size based on calculation of screen size
                    if (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 2.9) {
                           Log.e(TAG,"< 2.9 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, UnderDevlt_Scn_1_8inches.class);
                           startActivity(ScreenNotSupportedScreen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - DEVICE DETERMINED TO BE LESS THAN 2.9 INCHES - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 2.9) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 3.6)) {
                           Log.e(TAG,"2.9 to 3.6 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, UnderDevlt_Scn_2_9inches.class);
                           startActivity(ScreenNotSupportedScreen);     

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);                              
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 3.6) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 3.8)) {
                           Log.e(TAG,"3.6 to 3.8 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, UnderDevlt_Scn_2_9inches.class);
                           startActivity(ScreenNotSupportedScreen); 

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 3.8) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 4.0)) {
                           Log.e(TAG,"3.8 to 4.0 inch range called"); 

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_3_8inches.class);
                           startActivity(specific_login_screen);
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 4.0) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 4.28)) {
                           Log.e(TAG,"4.0 to 4.28 inch range called");

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_4inches.class);
                           startActivity(specific_login_screen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 4.28) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 4.8)) {
                           Log.e(TAG,"4.28 to 4.3 inch range called");

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_4_28inches.class);
                           startActivity(specific_login_screen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 4.8) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 4.9)) {
                           Log.e(TAG,"4.8 to 4.9 inch range called");

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_4_65inches.class);
                           startActivity(specific_login_screen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 4.9) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 5.5)) {
                           Log.e(TAG,"4.9 to 5.5 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, LogInScreen_4_98inches.class);
                           startActivity(ScreenNotSupportedScreen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 5.5) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 5.9)) {
                            Log.e(TAG,"5.5 to 5.9 inch range called");

                            Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_5_7inches.class);
                            startActivity(specific_login_screen);

                            Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 5.9) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 6.0)) {
                            Log.e(TAG,"5.9 to 6.0 inch range called");

                            Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_5_9inches.class);
                            startActivity(specific_login_screen);

                       Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 6.0) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 6.7)) {
                           Log.e(TAG,"6.0 to 7.0 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, UnderDevlt_Scn_Finish.class);
                           startActivity(ScreenNotSupportedScreen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 6.7) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 8.0)) {
                           Log.e(TAG,"7.0 to 8.0 inch range called");

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen.class);
                           startActivity(specific_login_screen);

                    }  else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 8.0) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen < 9.0)) {
                           Log.e(TAG,"8.0 to 9.0 inch range called");

                           Intent ScreenNotSupportedScreen = new Intent(SplashScreenFour.this, UnderDevlt_Scn_Finish.class);
                           startActivity(ScreenNotSupportedScreen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    } else if ((Device_Screen_Size_In_Inches_To_Display_Log_In_Screen >= 9.0) && (Device_Screen_Size_In_Inches_To_Display_Log_In_Screen <= 10.2)) {
                           Log.e(TAG,"> 9.0 to 10.2 inch range called");

                           Intent specific_login_screen = new Intent(SplashScreenFour.this, LogInScreen_10inches.class);
                           startActivity(specific_login_screen);

                           Log.e(TAG,"DEVICE NOT ACCOMODATED - ACTUAL DEVICE SIZE IS: " + Device_Screen_Size_In_Inches_To_Display_Log_In_Screen);
                    }
            }
        }
    };

    SplashThread.start();
}

与上述相反有没有办法处理众多设备?

这是我的清单文件:

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

    <uses-sdk
        android:minSdkVersion="3"
        android:maxSdkVersion="17"
        android:targetSdkVersion="8"/>

    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="applcs.test"
        android:label="AppLogInScreen_4inchesTestCase" />   

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

    <uses-feature android:name="android.hardware.telephony" android:required="false" />

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

    <application
        android:icon="@drawable/ic_App_launcher_96"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

        <uses-library android:name="android.test.runner" />   

        <!-- Splash screen -->
        <activity   
            android:name="AppSplashScreenFour"
            android:screenOrientation="landscape">
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Login screens -->
        <activity android:name="AppLogInScreen_3_6_inches"
            android:screenOrientation="landscape">
        </activity>     

        <activity android:name="AppLogInScreen_3_8inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppLogInScreen_4inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppLogInScreen_4_28inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppLogInScreen_4_65inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppLogInScreen_4_98inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppLogInScreen_5_7inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppLogInScreen_5_9inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppLogInScreen"
            android:screenOrientation="landscape">
        </activity>      

        <activity android:name="AppLogInScreen_10inches"
            android:screenOrientation="landscape"
            android:clearTaskOnLaunch="true">
        </activity>        

        <!-- Home screens -->       
        <activity android:name="AppHomeScreen_3_6inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppHomeScreen_3_8inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppHomeScreen_4inches"
            android:screenOrientation="landscape">
        </activity>      

        <activity android:name="AppHomeScreen_4_28inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppHomeScreen_4_65inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppHomeScreen_4_98inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppHomeScreen_5_7inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppHomeScreen_5_9inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppHomeScreen_7inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppHomeScreen_10inches"
            android:screenOrientation="landscape">
        </activity>     

        <!-- SW900 home screens -->
        <activity android:name="AppSW900AdminHomeScreen3_6inches"
            android:screenOrientation="landscape">
        </activity>

       <activity android:name="AppSW900AdminHomeScreen3_8inches"
            android:screenOrientation="landscape"> 
       </activity>          

       <activity android:name="AppSW900AdminHomeScreen4inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppSW900AdminHomeScreen4_28inches"
            android:screenOrientation="landscape"> 
        </activity>

       <activity android:name="AppSW900AdminHomeScreen4_65inches"
            android:screenOrientation="landscape"> 
       </activity>        

       <activity android:name="AppSW900AdminHomeScreen4_98inches"
            android:screenOrientation="landscape"> 
       </activity>        

       <activity android:name="AppSW900AdminHomeScreen5_7inches"
            android:screenOrientation="landscape"> 
       </activity>         

       <activity android:name="AppSW900AdminHomeScreen5_9inches"
            android:screenOrientation="landscape"> 
       </activity>        

       <activity android:name="AppSW900AdminHomeScreen7inches"
            android:screenOrientation="landscape"> 
       </activity>       

        <activity android:name="AppSW900AdminHomeScreen10inches"
            android:screenOrientation="landscape">
        </activity>

        <!-- Sales home screens -->       
        <activity android:name="AppSalesHomeScreen_4inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppSalesHomeScreen_7inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppSalesHomeScreen_10inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppProdBrochsHomeScreen"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppSales_ProdBrochsScreen_4inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity
            android:name="AppSales_ProdBrochsScreen_7inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppSales_ProdBrochsScreen_10inches"
            android:screenOrientation="landscape"> 
        </activity>

        <activity android:name="AppSales_ProdVideosScreen_4inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppSales_ProdVideosScreen_7inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppSales_ProdVideosScreen_10inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppSales_ProdPricingScreen_7inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppVideoScreen"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="AppPDFWebView"
            android:screenOrientation="landscape">
        </activity> 

        <activity android:name="AppPDFReader">
        </activity>

        <activity android:name="AppVideoHomeScreen"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppVideoHomeScreen2"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="AppListActivity"
            android:screenOrientation="landscape">
        </activity>

        <activity 
            android:name="AppVideoScreen_Tab0ProdCat1Prod1" 
            android:screenOrientation="landscape">
        </activity> 

        <activity 
            android:name="AppVideoScreen_Tab0ProdCat1Prod2" 
            android:screenOrientation="landscape"> 
        </activity>         

        <activity 
            android:name="AppVideoScreen_Tab0ProdCat1Prod3" 
            android:screenOrientation="landscape"> 
        </activity> 

        <activity 
            android:name="AppVideoScreen_Tab0ProdCat1Prod4" 
            android:screenOrientation="landscape"> 
        </activity> 

        <activity android:name="AndroidGridLayoutActivity"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="FullImageActivity"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="FullImageActivityTwo"
            android:screenOrientation="landscape">
        </activity>

        <!-- Under Devlt Scn -->
        <activity android:name="App_UnderDevlt_Scn"
            android:screenOrientation="landscape">
        </activity>  

        <activity android:name="App_UnderDevlt_Scn_1_8inches"
            android:screenOrientation="landscape">
        </activity>

        <activity android:name="App_UnderDevlt_Scn_4inches"
            android:screenOrientation="landscape">
        </activity>        

        <activity android:name="App_UnderDevlt_Scn_4_65inches"
            android:screenOrientation="landscape">
        </activity>               

        <activity android:name="App_UnderDevlt_Scn_5_7inches"
            android:screenOrientation="landscape">
        </activity>                     

        <activity android:name="App_UnderDevlt_Scn_7inches"
            android:screenOrientation="landscape">
        </activity>                   

        <activity android:name="App_UnderDevlt_Scn_2_9inches"
            android:screenOrientation="landscape">
        </activity>       

        <activity android:name="App_UnderDevlt_Scn_3_8inches"
            android:screenOrientation="landscape">
        </activity>           

        <activity android:name="App_UnderDevlt_Scn_10inches"
            android:screenOrientation="landscape">
        </activity>  

        <activity android:name="App_UnderDevlt_Scn_Finish"
            android:screenOrientation="landscape">
        </activity>  

        <activity
            android:name="AppWebViewActivity"
            android:theme="@android:style/Theme.NoTitleBar">
        </activity>

        <activity
            android:name="SampleActivity"
            android:label="@string/app_name" >
        </activity>  

    </application>

</manifest>

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

对于不同的屏幕尺寸,以下是应用程序中的资源目录列表,它为不同的屏幕尺寸和不同的位图绘图提供不同的布局设计,适用于小型,中型,高密度和超高密度屏幕。

 res/layout/my_layout.xml             // layout for normal screen size ("default")
 res/layout-small/my_layout.xml       // layout for small screen size
 res/layout-large/my_layout.xml       // layout for large screen size
 res/layout-xlarge/my_layout.xml      // layout for extra large screen size
 res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

 res/drawable-mdpi/my_icon.png        // bitmap for medium density
 res/drawable-hdpi/my_icon.png        // bitmap for high density
 res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

Manifest中的以下代码支持所有dpis。

<supports-screens android:smallScreens="true" 
      android:normalScreens="true" 
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true" />
相关问题