处理不同的屏幕尺寸 (Android)

Handling different screen sizes (Android)

自从开始 android 开发以来,我一直在使用以下可绘制文件夹结构...

drawable-mdpi 可绘制-hdpi 绘制-xhdpi drawable-xxhdpi

现在我的问题是这个,我在 2 种不同的设备上进行测试

联想 Tab S8 - 1980*1200 - xxhdpi
Nexus 5 - 1080*1920 - xxhdpi

lenovo tabs 的物理屏幕尺寸大约是 Nexus 的两倍,但它们都从 drawable-xxhdpi 中提取图像。

这意味着,如果我最初为 nexus 构建我的应用程序,那么当部署在选项卡上时,它将被大量未使用的空白包围 space。但是,如果我在 Tab 上开发得很好,它在 Nexus 上就会完全爆炸。

我已阅读此处

(http://www.techotopia.com/index.php/Handling_Different_Android_Devices_and_Displays)

添加名称为

的可绘制文件夹

drawable-sw200dp
drawable-sw600dp
drawable-sw800dp

处理不同的屏幕尺寸,但这只会让我的应用程序在 Nexus 上崩溃。标签没问题。

谁能解释为什么这是 and/or 处理 xxhdpi 类别中不同尺寸的解决方法?

更新:- XML 似乎导致问题的布局 (fragment_tutorial2.xml)...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
                android:paddingTop="20dip">
    <TextView
        android:id="@+id/hint"
        style="@style/TutorialTitle1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/tut_text2" />

    <TextView
        android:id="@+id/hint2"
        style="@style/TutorialTitle2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/hint"
        android:text="@string/tut_text3"/>

    <ImageView
        android:id="@+id/documentlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/documentlist" />

</RelativeLayout>

样式....

<style name="TutorialTitle1" parent="AppBaseTheme">
        <item name="android:textStyle">bold</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">50sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingTop">5dp</item>
    </style>

    <style name="TutorialTitle2" parent="AppBaseTheme">
        <item name="android:textStyle">bold</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingBottom">25dp</item>
    </style>

Logcat 使用名为 drawable-sw200dp 的文件夹发布到 Nexus 时 ...

2243-2243/uk.co.danbd.heswresources E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: uk.co.danbd.heswresources, PID: 2243
    android.view.InflateException: Binary XML file line #21: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at uk.co.danbd.heswresources.TutorialFragment.onCreateView(TutorialFragment.java:105)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
            at android.view.View.measure(View.java:17547)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at uk.co.danbd.heswresources.TutorialFragment.onCreateView(TutorialFragment.java:105)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
            at android.view.View.measure(View.java:17547)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

TutorialFrament.java(在 onCreateView() 内)....

switch (page) {
            case 0 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial1, container, false);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView1),R.anim.fadescalein,1000);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView2),R.anim.fadescalein,2000);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView3), R.anim.fadescalein, 2100);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView4), R.anim.fadescalein, 2200);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView5), R.anim.fadescalein, 2300);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView6), R.anim.fadescalein, 2400);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView7), R.anim.fadescalein, 2500);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView8), R.anim.fadescalein, 2600);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView9), R.anim.fadescalein, 2700);
                break;
            case 1 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial2, container, false); // line 105
                break;
            case 2 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial3, container, false);
                break;
            case 3 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial4, container, false);
                break;
            case 4 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial5, container, false);
                break;
            case 5 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial6, container, false);
                listView = (ListView) rootView.findViewById(R.id.list);
                if (prefs.getString("type", "Acute") == "Acute") {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.tutorial_layout_row, valuesAcute);
                    listView.setAdapter(adapter);
                } else {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.tutorial_layout_row, valuesMentalHealth);
                    listView.setAdapter(adapter);
                }
                listView.setOnItemClickListener(listPairedClickItem);
                break;
        }
        return rootView;

fragment_tutorial1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
                android:paddingTop="20dip" >

    <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="@dimen/logo_margin_top"
        android:src="@drawable/doctorslogo" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ecg" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView2"
        android:layout_alignTop="@id/imageView2"
        android:src="@drawable/handwash" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/imageView2"
        android:layout_alignRight="@id/imageView2"
        android:src="@drawable/chart" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imageView2"
        android:layout_alignTop="@id/imageView2"
        android:layout_marginTop="-40dp"
        android:src="@drawable/cross" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView5"
        android:layout_toLeftOf="@id/imageView2"
        android:src="@drawable/steph" />    

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imageView2"
        android:layout_alignBottom="@id/imageView2"
        android:layout_marginBottom="-49dp"
        android:src="@drawable/hygieia" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView2"
        android:layout_alignLeft="@id/imageView2"
        android:src="@drawable/hospital" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView2"
        android:layout_below="@id/imageView2"
        android:src="@drawable/droplet" />

</RelativeLayout>

支持不同的设备屏幕对 Android 开发人员来说是一个挑战,直到您知道该怎么做。

要支持不同的设备,您可以执行以下操作(drawables):

在普通项目中,您有以下可绘制文件夹 - 请记住,这与您希望在应用程序中支持的设备无关:

  • 可绘制/
  • drawable-mdpi/
  • drawable-hdpi/
  • drawable-xhdpi/
  • drawable-xxhdpi/
  • drawable-xxxhdpi/

为上述文件夹生成适当大小的图像后,您的应用 android 将自动为相应设备(例如 Nexus 4、5 英寸、7 英寸、10 英寸)加载正确的资源.

提供对不同屏幕尺寸(布局)的支持

例如使用您的开发设备(可以是 Nexus 4),然后您可以为您想要支持的其他屏幕尺寸创建布局文件夹。例如,我会创建以下文件夹:

  • layout-sw300dp/
  • layout-sw400dp/
  • layout-sw600dp/
  • layout-sw800dp/

在上面的布局文件夹中,您可以分别设置 xml files/views 的样式,这样使用不同屏幕的用户仍然会看到您希望他们看到的应用。

同样,android 系统将加载正确的资源。

希望这对您有所帮助,让我知道进展如何!祝你好运!

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

是这个原因,好像是你的位图太大了。

@AndrewBramwell

你的 logcat 抛出什么

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

http://developer.android.com/reference/java/lang/OutOfMemoryError.html

对于Android来说太大了,它会耗尽内存。所以,最好resize将图像缩小。

并将其添加到您的 ImageView

android:adjustViewBounds="true"

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Must be a boolean value, either "true" or "false".

您可以在清单应用程序标记中设置 android:largeHeap="true"