gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:33 25 4
gpt4 key购买 nike

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

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

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

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

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

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

enter image description here enter image description here

我在这里读过

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

添加一个名为drawable的文件夹

drawable-sw200dp
drawable-sw600dp
绘制-sw800dp

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

谁能解释为什么这是和/或处理 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>

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

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>

最佳答案

@安德鲁布拉姆威尔

你的 logcat 抛出什么

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

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

对于 Android 来说太大了,它会耗尽内存。所以,最好调整图像到更小的尺寸。

并将其添加到您的 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".

您可以在您的 list Application Tag 中设置 android:largeHeap="true"

关于android - 处理不同的屏幕尺寸 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com