【问题标题】:How to get safe area in android for notch devices如何在 android 中为 notch 设备获取安全区域
【发布时间】:2020-01-14 07:19:04
【问题描述】:

我需要处理缺口设备的布局。 我知道在IOS。 我可以使用 Ios 中的安全区域来处理它。 但是在android中有没有办法实现这个?

【问题讨论】:

    标签: android ios android-layout safearealayoutguide safeareaview


    【解决方案1】:

    为此,您需要 DisplayCutout 对象 在您的 Activity 类中(在 onCreate() 中):

    WindowManager.LayoutParams lp = this.getWindow().getAttributes();
    
    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;    
    
    WindowInsets windowInsets = this.getWindow().getDecorView().getRootView().getRootWindowInsets();
    
    DisplayCutout displayCutout = windowInsets.getDisplayCutout();
    int bottom = displayCutout.getSafeInsetBottom()
    

    您可以在此处找到有关此课程的更多信息: https://developer.android.com/guide/topics/display-cutout 和这里: https://blog.felgo.com/app-dev-tips-for-devices-with-edge-to-edge-screens-2020

    【讨论】:

      【解决方案2】:

      这将给出一个 Rect,其安全区域位于顶部、底部、左侧和右侧。它还具有 API 兼容性检查

       public static Rect getSafeArea(@NonNull Activity activity) {
          final Rect safeInsetRect = new Rect();
          if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            return safeInsetRect;
          }
      
          final WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
          if (windowInsets == null) {
            return safeInsetRect;
          }
      
          final DisplayCutout displayCutout = windowInsets.getDisplayCutout();
          if (displayCutout != null) {
            safeInsetRect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
          }
      
          return safeInsetRect;
       }
      

      【讨论】:

      • 不工作,例如在三星 A40 上。 displayCutout.getSafeInsetBottom() 返回 0 但这款手机有导航栏的凹槽。
      【解决方案3】:

      你可以用这个:

      <style name="ActivityTheme">
        <item name="android:windowLayoutInDisplayCutoutMode">
          shortEdges <!-- default, shortEdges, never -->
        </item>
      </style>
      

      或旗帜

      class MainActivity : AppCompatActivity() {
      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              // or add <item name="android:windowTranslucentStatus">true</item> in the theme
              window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
      
              val attrib = window.attributes
              attrib.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
          }
      }
      

      样式为

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      
          <!-- Adding fullscreen will just hide the status bar -->
          <!-- <item name="android:windowFullscreen">true</item> -->
      </style>
      

      【讨论】:

        猜你喜欢
        • 2014-06-14
        • 2021-01-03
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        相关资源
        最近更新 更多