片段取向变化 - 比景观更好的测试 [英] Fragment Orientation Change - Better Test than for Landscape

查看:94
本文介绍了片段取向变化 - 比景观更好的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在风景模式,我被一个活动控制的两个FrameLayouts,利用两个片段。在肖像模式,我被一个活动一的FrameLayout控制,在选择线路我调用另一个活动使用的细节片段显示详细信息

In 'Landscape' mode I have two FrameLayouts controlled by One Activity and utilizing two Fragments. In 'Portrait' mode I have one FrameLayout controlled by One Activity, on Selecting a Line I call another activity to display the detail using a detail fragment

在肖像详细活动我有以下检查定位在OnCreate()方法。

In the 'Portrait' detail activity I have the following check for orientation in the onCreate() method.

如果(getResources()。getConfiguration()。方向== Configuration.ORIENTATION_LANDSCAPE){     完();     返回; }

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; }

以上的作品很好,但,

有一个问题出现了,虽然当我有一个'小'的设备。在这种情况下,风景模式,我不希望这两个片段来看,而我要表现得象'画像'视图。然而,由于该装置实际上是在景观时的细节活性启动它自动完成。

A problem arises though when I have a 'small' device. In this case in 'Landscape' mode I don't want the two fragment view, rather I want to behave like the 'portrait' view. However since the device is in fact in 'Landscape' when the detail activity launches it automatically finishes.

所以,问题是什么是处理这个的最好方法是什么?

So the questions is what is the best way of handling this?

推荐答案

,或创建一个布尔值(从谷歌IO 2012)自定义资源

or create a custom resource with a bool value (from google io 2012)

<!-- in your values/custom.xml -->
<resources>
    <bool name="small_screen">true</bool>  
    <bool name="normal_screen">false</bool>  
</resources>

<!-- in your values-sw320dp/custom.xml -->
<resources>
    <bool name="small_screen">false</bool>  
    <bool name="normal_screen">true</bool>
</resources>

注意:你必须定义一个最小的屏幕宽度(sw320dp),这些你会考虑屏幕不小(<一href="http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources"相对=nofollow>更多信息链接)

NOTE: You have to define a minimum screenwidth (sw320dp) for which you will consider a screen not to be small (link with more info)

的好处是,你可以在运行时读取和放大器的设定值;你可以有特殊的资源预选赛特殊情况...例如:你可以通过调用您的活动在运行时读取此值:

The advantage is that you can read this value at runtime & you can have special cases for special resource qualifiers... E.g. you can read this value at runtime by calling in your activity:

if(getResources().getBoolean(R.bool.small_screen)) {
    // You have a screen which is < 320dp
} else {
    // You have a screen which is >= 320dp
}

您甚至可以使用这个布尔资源在你的清单像这样,开始了完全不同的活动小屏幕

You can even use this boolean resource in your manifest like so, to start a completely different activity for small screens

<activity android:name="SmallScreenActivity" 
          android:enabled="@bool/small_screen">  <!-- ENABLE FOR SMALL SCREEN -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="NormalActivity" 
          android:enabled="@bool/normal_screen"> <!-- ENABLE FOR OTHER -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

此方式,您可以简单地使用一个活动的正常情况下(机器人:启用=@布尔/ normal_screen),并使用一个特殊的活动小屏幕的android:启用=@布尔/ small_screen

This way you can simply use an Activity for the normal case (android:enabled="@bool/normal_screen") and use a special activity for small screen android:enabled="@bool/small_screen"

警告:因为蜂窝在较新的设备,这个方法将无法正常工作 <一个href="http://damianflannery.word$p$pss.com/2011/10/16/architecting-a-single-apk-app-to-handle-phones-and-tablets-on-android/"相对=nofollow>您可以阅读为什么不允许再此方法或<一href="http://stackoverflow.com/questions/13202805/how-to-specify-activities-that-are-only-for-phones-or-tablets-on-android/13202806#13202806">read关于工作类似的解决方案

这篇关于片段取向变化 - 比景观更好的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆