简体   繁体   English

Android-处理不同的屏幕尺寸

[英]Android - dealing with different screen sizes

I suspect this is a well dealt with topic as I've read several posts as indicated below but I seem to be encountering screen size issues. 我怀疑这是一个处理得很好的主题,因为我阅读了以下几篇文章,但似乎遇到了屏幕尺寸问题。

For example for the Asus FonePad using my calculation in the code below I calculate the screen size to be 6.833 inches but on researching further on the device it is advertised as a 7 inch device. 例如,对于华硕FonePad,使用下面的代码计算得出的屏幕尺寸为6.833英寸,但在进一步研究该设备时,它的广告尺寸为7英寸。 This leads to me picking up the "wrong" screen size. 这导致我选择“错误的”屏幕尺寸。

Below is the code indicating how I'm calculating the screen size of the devices using my app and subsequently calling the correct layout: 下面的代码指示我如何使用我的应用程序计算设备的屏幕尺寸,然后调用正确的布局:

   // 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();
}

As opposed to the above Is there any way to deal with the numerous array of devices? 与上述相反的是,有没有办法处理众多设备?

Here's my manifest file: 这是我的清单文件:

    <?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>

I appreciate any assistance. 感谢您的协助。

For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. 对于不同的屏幕尺寸,以下是应用程序中的资源目录列表,该应用程序为不同的屏幕尺寸提供了不同的布局设计,并为小,中,高和超高密度屏幕提供了不同的位图可绘制对象。

 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

The following code in the Manifest supports all dpis. 清单中的以下代码支持所有dpi。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM