2

私はアプリを開発していますが、4 インチから 10 インチのほとんどの Android デバイスに対応する必要があり、デバイスに基づいてスケーリングするユニバーサル画面レイアウトを作成する方法があるかどうか疑問に思っていました。

これは、4 インチ、5 インチなどの画面カテゴリごとに異なるレイアウトとそれぞれのアクティビティを開発する現在のロジックとは対照的に、より良い方法です。

以下に示すように、私は DisplayMetrics フィールドとピタゴラスの定理を利用して画面サイズを決定します。たとえば、デバイスの高さを見つけるには、次の計算を行います。

double yDeviceScreenSize = Math.pow(DeviceScreenSize.heightPixels/DeviceScreenSize.ydpi,2);

上記のコード行は基本的に、ピクセル単位のディスプレイの絶対高さを、Y 次元の画面の 1 インチあたりの正確な物理ピクセルで割り、その後に値を 2 乗したものです。

同様の方法で、同じ方法で幅の数値を取得します。これを以下に示します。

double xDeviceScreenSize = Math.pow(DeviceScreenSize.widthPixels/DeviceScreenSize.xdpi,2);

上記の計算に続いて、以下に示すように平方根の値を取得します。これを使用して、同じ画面サイズのデバイスで開発およびテストしたものを選択します。

DeviceScreenSizeInInchesToDisplayLogInScreen = Math.sqrt(xDeviceScreenSize + yDeviceScreenSize);

以下に示すように、

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

以下は、スプラッシュスクリーンの作成時に呼び出すコード画面決定メソッドです。

   // 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
    SightsandSoundsSplashThread = 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 xDeviceScreenSize = 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 yDeviceScreenSize = Math.pow(DeviceScreenSize.heightPixels/DeviceScreenSize.ydpi,2);

                    // Obtain screen size using pythagoras theorem
                    DeviceScreenSizeInInchesToDisplayLogInScreen = Math.sqrt(xDeviceScreenSize + yDeviceScreenSize);

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

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

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

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

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

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

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

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

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

                           Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen4inches.class);
                           startActivity(loginscreen);

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

                           Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen428inches.class);
                           startActivity(loginscreen);

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

                           Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen465inches.class);
                           startActivity(loginscreen);

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

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

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


                    } else if ((DeviceScreenSizeInInchesToDisplayLogInScreen >= 5.5) && (DeviceScreenSizeInInchesToDisplayLogInScreen < 5.9)) {
                            Log.e(TAG,"5.5 to 5.9 inch range called");

                            Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen57inches.class);
                            startActivity(loginscreen);

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

                            Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen59inches.class);
                            startActivity(loginscreen);

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

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

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

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

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

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

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

                           Intent loginscreen = new Intent(SplashScreenFour.this, LogInScreen10inches.class);
                           startActivity(loginscreen);

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

    SightsandSoundsSplashThread.start();
}

以下は、私の位置を示す私のマニフェストです

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bge.applcs.dsa"
    android:versionCode="7"
    android:versionName="1.07">

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


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

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

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

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

</manifest>

以下は、私が作成したレイアウトの例です。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llbgedsafinccalclayout7inches"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/imgplainbackground"
    android:orientation="horizontal">

   <RelativeLayout 
        android:id="@+id/rlbgedsafinccalclayout7inches"
        android:layout_width="925dp"
        android:layout_height="300dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/imgbgedsafinccalcmainbitbgnd2">

     <!-- <TextView
        android:id="@+id/tvcurrencysign"
        android:layout_width="wrap_content"
        android:layout_height="31dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="330dp"
        android:paddingTop="4dp"
        android:textSize="18sp"
        android:textColor="#FFD700"
        android:background="#07000000"/> -->        

    <EditText
        android:id="@+id/ettotalvalue7inches"
        android:singleLine="true"
        android:layout_width="215dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_marginLeft="389dp"
        android:maxLength="9"
        android:layout_gravity="center_vertical|right"
        android:background="#07000000"
        android:textColor="#FFD700"
        android:textSize="19sp"
        android:ems="10"
        android:inputType="numberDecimal|numberSigned"
        android:digits="0123456789."
        android:selectAllOnFocus="true"/>

     <ImageButton
        android:id="@+id/btngenerate7inches"
        android:layout_width="276dp"
        android:layout_height="54dp"
        android:layout_marginLeft="45dp"
        android:layout_toRightOf="@+id/ettotalvalue7inches"
        android:background="@drawable/btngeneratebgnd"/>    

     <ImageButton
        android:id="@+id/btntwoyears7inches"
        android:layout_width="73dp"
        android:layout_height="47dp"
        android:layout_marginTop="56dp"
        android:layout_marginLeft="5dp"
        android:layout_below="@+id/ettotalvalue7inches"
        android:background="@drawable/btntwoyears"/>    

     <ImageButton
        android:id="@+id/btntthreeyears7inches"
        android:layout_width="73dp"
        android:layout_height="47dp"
        android:layout_marginTop="56dp"
        android:layout_marginLeft="2dp"
        android:layout_below="@+id/ettotalvalue7inches"
        android:layout_toRightOf="@+id/btntwoyears7inches"
        android:background="@drawable/btnthreeyears"/>  

     <ImageButton
        android:id="@+id/btnfouryears7inches"
        android:layout_width="73dp"
        android:layout_height="47dp"
        android:layout_marginTop="56dp"
        android:layout_marginLeft="1dp"
        android:layout_below="@+id/ettotalvalue7inches"
        android:layout_toRightOf="@+id/btntthreeyears7inches"
        android:background="@drawable/btnfouryears"/>   

     <ImageButton
        android:id="@+id/btnfiveyears7inches"
        android:layout_width="74dp"
        android:layout_height="47dp"
        android:layout_marginTop="56dp"
        android:layout_marginLeft="1dp"
        android:layout_below="@+id/ettotalvalue7inches"
        android:layout_toRightOf="@+id/btnfouryears7inches"
        android:background="@drawable/btnfiveyears"/>

     <TextView
        android:id="@+id/tvweeklypayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="22dp"
        android:layout_marginLeft="310dp"
        android:layout_below="@+id/btntwoyears7inches"
        android:paddingTop="6dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>         

     <TextView
        android:id="@+id/tvfirstpayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="21dp"
        android:layout_marginLeft="310dp"
        android:layout_below="@+id/tvweeklypayment"
        android:paddingTop="6dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>

     <TextView
        android:id="@+id/tvmonthlypayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="22dp"
        android:layout_marginLeft="310dp"
        android:layout_below="@+id/tvfirstpayment"
        android:paddingTop="6dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>        

    <TextView
        android:id="@+id/tvtotalcostpayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="22dp"
        android:layout_marginLeft="351dp"
        android:layout_below="@+id/btnfiveyears7inches"
        android:layout_toRightOf="@+id/tvweeklypayment"
        android:paddingTop="6dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>        

     <TextView
        android:id="@+id/tvtaxreliefpayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="22dp"
        android:layout_marginLeft="351dp"
        android:layout_below="@+id/tvtotalcostpayment"
        android:layout_toRightOf="@+id/tvfirstpayment"
        android:paddingTop="6dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>        

     <TextView
        android:id="@+id/tvnetcostpayment"
        android:layout_width="122dp"
        android:layout_height="25dp"
        android:layout_marginTop="21dp"
        android:layout_marginLeft="351dp"
        android:layout_below="@+id/tvtaxreliefpayment"
        android:layout_toRightOf="@+id/tvmonthlypayment"
        android:paddingTop="4dp"
        android:paddingLeft="0dp"
        android:textSize="12sp"
        android:textColor="#FFD700"
        android:background="#07000000"/>       

     <TextView
        android:id="@+id/tvnumbofmonthlypayments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="19dp"
        android:layout_below="@+id/tvtaxreliefpayment"
        android:layout_marginLeft="15dp"
        android:textSize="19sp"
        android:text="24"
        android:textColor="#FFD700"
        android:background="#07000000"/>         

    </RelativeLayout>   

    <RelativeLayout 
        android:id="@+id/rlnotes7inches"
        android:layout_width="925dp"
        android:layout_height="300dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/imgnotesbgnd"> 

    </RelativeLayout>

    <TextView
        android:id="@+id/tvinvalidentry7inches"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="370dp"
        android:layout_below="@+id/rlbgedsafinccalclayout7inches"
        android:textSize="25sp"
        android:textColor="#FF0000"
        android:background="#07000000"/>  

    <ImageButton
        android:id="@+id/btninfo7inches"
        android:layout_width="65dp"
        android:layout_height="60dp"
        android:layout_marginTop="82dp"
        android:layout_marginLeft="12dp"
        android:layout_below="@+id/rlbgedsafinccalclayout7inches"
        android:background="@drawable/btninfobgnd"/>    

     <TextView
         android:id="@+id/tvdateandtime7inches"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="95dp"
         android:layout_marginLeft="515dp"
         android:layout_below="@+id/rlbgedsafinccalclayout7inches"
         android:layout_toRightOf="@+id/btninfo7inches"
         android:background="#07000000"
         android:textColor="#FFFFFF"
         android:textSize="25sp"
         android:textStyle="bold" />

     <DigitalClock
        android:id="@+id/digitalclock7inches"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="95dp"
        android:background="#07000000"
        android:layout_below="@+id/rlbgedsafinccalclayout7inches"
        android:layout_toRightOf="@+id/tvdateandtime7inches"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        android:textStyle="bold"/>  

</RelativeLayout>

助けてくれてありがとう。

4

1 に答える 1

0

複数の画面のサポートを読みましたか? さまざまな画面サイズに対処する方法に関する多くの情報があります。

通常、これは複数のレイアウトを作成する場合にすぎません。正確な数は、デバイス間でレイアウトをどの程度変えたいかによって異なります。

これらのレイアウトは、orなどのres/layout-*/フォルダーに配置されます(サポート対象の詳細については、ドキュメントを参照してください)。res/layout-xhdpi/res/layout-land/res/layout-land-sw800dp/

Android システムは、提供された画面の向き、ピクセル密度、およびサイズに応じて、適切なレイアウトを自動的に選択します。

あなたが提供したレイアウトの場合、従来のデスクトップ UI デザインで一般的であるように、幅/高さ/マージンに多くの絶対測定値を使用しています。しかし、ご存じのように、モバイル デバイスでは、このアプローチはスケーリングしません (しゃれは意図されていません!) - 他のすべてのタイプの画面/方向は、これらの測定値に適合しません。UIデザインへのアプローチ方法を変える必要があります。

あなたの場合、あなたのレイアウトは、2 年から 5 年の期間にわたって定期的な毎週または毎月の支払いを必要とするサブスクリプションに関連して、ユーザーが入力する必要があるフォームを表しているようです。要素は、使用可能な画面領域を最大限に活用するために両方の軸に配置されており、これがさらなる問題を引き起こしています。

まず、レイアウトを 2 つのタイルに分割することから始めることができます。左側には「生成」ボタンと 4 つの「年」ボタンがあり、右側には初回/週ごと/月ごとの支払いの概要があります。これらは、水平方向モードRelativeLayoutの外側のセット内の 2 つのコンテナーである可能性があります。LinearLayout(次に、縦向きの画面のレイアウトを作成できます。唯一の違いは、LinearLayoutが垂直方向に設定されているため、2 つのタイルが 1 つの垂直列として表示されます)。

2 つの子レイアウトのそれぞれについて、ボタンとラベルが親に対して相対的に配置されていることを確認します。要素に絶対的な幅や高さを指定することは避けてWRAP_CONTENTくださいMATCH_PARENT。マージンについても同じ - マージンは、隣接する要素間に小さなギャップを作成するのに最適であり、画面上のアイテムの配置にはめったに使用しないでください。テキストのサイズには必ず DP 寸法を使用してください。

この相対的な配置と測定の使用は、すべての UI 要素が表示されている画面に反応するように保つのに役立ちます。いくつかの異なる画面サイズと向きで UI をテストし続けて、それらがどのように見えるかを把握してください。

これで始められるはずです。これは、Android のさまざまなレイアウトがどのように設計されており、複数の画面で作業できるようになっているのか、そして絶対配置がいかに悪魔であるかを理解するためのケースです!

于 2013-09-20T10:24:07.230 に答える