【问题标题】:Fragment Orientation Change - Better Test than for Landscape片段方向更改 - 比横向更好的测试
【发布时间】:2012-09-20 13:27:36
【问题描述】:

在“横向”模式下,我有两个 FrameLayouts,由一个 Activity 控制并使用两个 Fragment。在“纵向”模式下,我有一个由一个活动控制的 FrameLayout,在选择一条线时,我调用另一个活动以使用细节片段显示细节

在“人像”细节活动中,我在 onCreate() 方法中检查了以下方向。

如果 (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 结束(); 返回; }

上面的效果很好,但是,

当我有一个“小型”设备时,就会出现问题。在这种情况下,在“横向”模式下,我不想要两个片段视图,而是希望表现得像“纵向”视图。但是,由于在详细活动启动时设备实际上处于“横向”状态,因此它会自动完成。

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

【问题讨论】:

    标签: android android-activity orientation fragment


    【解决方案1】:

    或使用 bool 值创建自定义资源(来自 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),您认为屏幕不会太小 (link with more info)

    优点是您可以在运行时读取此值,并且您可以有特殊资源限定符的特殊情况...例如您可以通过调用您的活动在运行时读取此值:

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

    您甚至可以像这样在清单中使用这个布尔资源,为小屏幕启动一个完全不同的活动

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

    这样你就可以简单地为普通情况使用一个 Activity (android:enabled="@bool/normal_screen") 并为小屏幕使用一个特殊的 Activity android:enabled="@bool/small_screen"

    警告:此方法不适用于自蜂窝以来的较新设备。 You can read why this method is not allowed anymoreread about working similar solution

    【讨论】:

      【解决方案2】:

      在检查方向之前,请执行额外的屏幕尺寸检查。考虑到小型设备的宽度为 500 像素,高度为 600 像素,您会这样做。

      Display display = getWindowManager().getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); i
      int width = size.x; 
      int height = size.y;
      if ( width > 500 && height > 600 && 
          getResources().getConfiguration().orientation == 
          Configuration.ORIENTATION_LANDSCAPE)
      { 
           finish(); 
           return; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-13
        • 2011-07-28
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多