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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23606448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:18:13  来源:igfitidea点击:

how to auto resize, compatible, adjust screen size for all android devices

androidandroid-layoutandroid-xmlandroid-screen-supportandroid-resolution

提问by Smith

I am creating a simple quiz test app "Target SDK API 16 (4.1 Jelly Beans)" Screen 3.7 (480x800 hdpi).

我正在创建一个简单的测验测试应用程序“Target SDK API 16 (4.1 Jelly Beans)” Screen 3.7 (480x800 hdpi)。

This app looks great on 3.7 (480x800), but when I run this on another screening device like 2.7 (240x320), 7.0 (1024x600), 10.1 (1280x800) its screen resolution gets messed up or looks bad.

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

For better understanding see screenshot:

为了更好地理解,请参见截图:

2.7 (240x320)

2.7 (240x320)

http://postimg.cc/image/m3sob88mp/

http://postimg.cc/image/m3sob88mp/

3.7 (480x800)

3.7 (480x800)

http://postimg.cc/image/wf513w0c1/

http://postimg.cc/image/wf513w0c1/

7.0 (1024x600)

7.0 (1024x600)

http://postimg.cc/image/fc298djn5/

http://postimg.cc/image/fc298djn5/

10.1 (1280x800)

10.1 (1280x800)

http://postimg.cc/image/isk5gon7p/

http://postimg.cc/image/isk5gon7p/

I want this to be compatible / look perfect with all screen sizes just like it looks in 3.7 (480x800)

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

How to auto resize, make compatible, adjust screen size for all android devices so it looks perfect in every screen resolution?

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

Or will I have to create a different app or different screen sizes?

还是我必须创建不同的应用程序或不同的屏幕尺寸?

What I tried to make screen compatible is: added these lines to "AndroidManifest.xml"

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

<supports-screens>

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

    </supports-screens>

AndroidManifest.xml

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>

first_screen.xml(this is the screen that is shown in the screenshot)

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>

回答by Leandros

There are some things which are crucial, if you want to support different screen sizes:

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

  • Use different drawables for every screen density bucket (drawables-hdpi, drawables-xhdpi, etc)
  • Use dpinstead of pxas unit for size.
  • Avoid using absolute sizes, use margins and let Android scale it accordingly.
  • 使用不同的绘图资源,为每个画面密度桶(drawables-hdpidrawables-xhdpi,等)
  • 使用dp而不是px作为大小单位。
  • 避免使用绝对尺寸,使用边距并让 Android 相应地缩放。

You can read more about supporting multiple screen sizes here.

您可以在此处阅读有关支持多种屏幕尺寸的更多信息。

Edit:

编辑:

To use different button / font sizes and margins, you should use the dimens.xml.

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

res/values-hdpi/dimens.xml
res/values-xhdpi/dimens.xml

Example dimens.xml:

示例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>

回答by Bashir ahmad

use linear layout as your main layout and divide it into part three four or five ,using weightSum, upto you how you want divide it and then use other sub linear layouts and assign weightSum to each sub linear Layout from the total main layout. manage UI widgets in the sub layouts. here is the example below

使用线性布局作为您的主要布局并将其分为三四或五部分,使用 weightSum,取决于您想要如何划分它,然后使用其他子线性布局并将 weightSum 分配给总主布局中的每个子线性布局。管理子布局中的 UI 小部件。这是下面的例子

回答by Samir Elekberov

You should use constraint layout. You can create constraints between objects...And should use "match_constraints","match_parent","wrap_content" for size..And also use margins.. You can learn How to Build a Responsive UI with ConstraintLayout

您应该使用约束布局。您可以在对象之间创建约束......并且应该使用“match_constraints”、“match_parent”、“wrap_content”作为大小......并且还使用边距......你可以学习如何使用 ConstraintLayout 构建响应式 UI