如何自动调整大小,兼容,调整所有Android设备的屏幕大小

Smi*_*ith 5 android android-layout android-xml android-resolution android-screen-support

我正在创建一个简单的测验测试应用程序"Target SDK API 16(4.1 Jelly Beans)"屏幕3.7(480x800 hdpi).

这个应用程序在3.7(480x800)上看起来很棒,但是当我在另一个屏幕设备上运行时,如:2.7(240x320),7.0(1024x600),10.1(1280x800),它的屏幕分辨率变得混乱或看起来很糟糕.

为了更好地理解看截图:

2.7(240x320)

http://postimg.org/image/m3sob88mp/

3.7(480x800)

http://postimg.org/image/wf513w0c1/

7.0(1024x600)

http://postimg.org/image/fc298djn5/

10.1(1280x800)

http://postimg.org/image/isk5gon7p/

我希望这与所有屏幕尺寸兼容/外观完美,就像在3.7(480x800)中看起来一样

如何自动调整大小,兼容,调整所有Android设备的屏幕尺寸,使其在每个屏幕分辨率下看起来都很完美.

或者我必须创建不同的应用程序或不同的屏幕大小.

我试图使屏幕兼容的是:将这些行添加到"AndroidManifest.xml"

<supports-screens>

        android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"

    </supports-screens>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.usd.quiztest.Logo"
            android:label="@string/app_name"            
            android:theme="@android:style/Theme.Black.NoTitleBar" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />               
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name="com.usd.quiztest.First"
            android:label="@string/app_name" >
        </activity>                
        <activity
            android:name="com.usd.quiztest.Q1"
            android:label="@string/app_name" >
        </activity>        
         <activity
            android:name="com.usd.quiztest.Q2"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q3"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q4"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q5"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.FinalPage"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Score"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

first_screen.xml(这是屏幕截图中显示的屏幕)

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:contentDescription="ql"
        android:gravity="center"
        android:src="@drawable/ql" />

    <Button
        android:id="@+id/start_button"
        android:layout_width="254dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Start Quiz Test"
        android:textColor="#000000" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Lea*_*ros 8

如果您想支持不同的屏幕尺寸,有些事情至关重要:

  • 使用不同的绘图资源,为每个画面密度桶(drawables-hdpi,drawables-xhdpi,等)
  • 使用dp而不是px作为单位的大小.
  • 避免使用绝对大小,使用边距并让Android相应地缩放它.

您可以在此处详细了解支持多种屏幕尺寸.

编辑:

要使用不同的按钮/字体大小和边距,您应该使用dimens.xml.

res/values-hdpi/dimens.xml
res/values-xhdpi/dimens.xml
Run Code Online (Sandbox Code Playgroud)

示例dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)