我正在开发一个应用程序,我必须满足 4 英寸到 10 英寸之间的绝大多数 android 设备的需求,我想知道是否有一种方法可以创建一个可以根据设备进行缩放的通用屏幕布局。
这将是一种更好的方式,而不是我目前为每个屏幕类别(即 4 英寸、5 英寸等)开发不同布局和相应活动的逻辑。
如下所示,我利用 DisplayMetrics 字段和毕达哥拉斯定理来确定屏幕尺寸,例如,找到设备的高度我进行以下计算:
double yDeviceScreenSize = Math.pow(DeviceScreenSize.heightPixels/DeviceScreenSize.ydpi,2);
上面的代码行本质上是显示器的绝对高度(以像素为单位)除以 Y 维度上每英寸屏幕的确切物理像素,然后对值进行平方。
以类似的方式,我以相同的方式获得宽度图。如下所示:
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>
谢谢你的帮助。