Android:“缩放以填充屏幕”和“拉伸以填充屏幕”

Posted

技术标签:

【中文标题】Android:“缩放以填充屏幕”和“拉伸以填充屏幕”【英文标题】:Android: "zoom to fill screen" and "strech to fill screen" 【发布时间】:2012-01-31 13:17:02 【问题描述】:

当我在zoom to fill screenstretch to fill screen 之间切换时,我的应用程序将崩溃。顺便说一句,我注意到市场上的很多应用程序都有这个问题。有谁知道如何避免这个问题。

这是日志:

E/androidRuntime(24475): FATAL EXCEPTION: main
E/AndroidRuntime(24475): java.lang.RuntimeException: Unable to start activity ComponentInfocom.macchetv.api/com.macchetv.activity.HomeActivity: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.AbsListView$SavedState
E/AndroidRuntime(24475):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
E/AndroidRuntime(24475):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
E/AndroidRuntime(24475):        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3191)
E/AndroidRuntime(24475):        at android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(24475):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
E/AndroidRuntime(24475):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24475):        at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(24475):        at android.app.ActivityThread.main(ActivityThread.java:4126)
E/AndroidRuntime(24475):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24475):        at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(24475):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(24475):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(24475):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24475): Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.AbsListView$SavedState
E/AndroidRuntime(24475):        at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1650)
E/AndroidRuntime(24475):        at android.view.View.dispatchRestoreInstanceState(View.java:8341)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038)
E/AndroidRuntime(24475):        at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475):        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475):        at android.view.View.restoreHierarchyState(View.java:8320)
E/AndroidRuntime(24475):        at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1642)
E/AndroidRuntime(24475):        at android.app.Activity.onRestoreInstanceState(Activity.java:898)
E/AndroidRuntime(24475):        at android.app.Activity.performRestoreInstanceState(Activity.java:870)
E/AndroidRuntime(24475):        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1101)
E/AndroidRuntime(24475):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1796)
E/AndroidRuntime(24475):        ... 12 more

【问题讨论】:

LogCat 是怎么说的 发布代码以便我们找出您的问题 问题是我不知道是代码的哪一部分导致了问题。 【参考方案1】:

似乎我找到了另一种方法,阅读您的帖子:在布局上为小部件提供不同的 id,根据方向实例化,如下所示:

int ot = getResources().getConfiguration().orientation;
switch (ot)

    case  Configuration.ORIENTATION_LANDSCAPE:
        lvListe = (HorizontalListView)mainView.findViewById(R.id.hlvListe);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        lvListe = (ListView)mainView.findViewById(R.id.lvListe);
        break;
    default:
        break;

【讨论】:

【参考方案2】:

问题出在 xml 布局内部,我在 layout-normal 中使用了一个滚动视图,在 layout-xlarge 中使用了一个 listView 来获取相同的 viewId。当 Android 尝试恢复视图的状态时,这将导致应用程序崩溃。

一个很好的解决方法是覆盖方法onSaveInstaceStateonRestoreInstaceState,这样可以避免classCastException

【讨论】:

以上是关于Android:“缩放以填充屏幕”和“拉伸以填充屏幕”的主要内容,如果未能解决你的问题,请参考以下文章

Android开发和JAVA开发有啥区别

FrameLayout 中的 'android:foreground' 和 'android:foregroundGravity' 如何影响其外观?

android:windowBackground 和 android:colorBackground 之间的区别?

如何建立与 Android studio和 gradle android 库

Android Webview 和Javascript交互,实现Android和JavaScript相互调用

android: android中dipdppxsp和屏幕密度