0

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

4

3 回答 3

1

这将给出一个在顶部、底部、左侧和右侧具有安全区域的 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;
 }
于 2020-11-13T06:24:27.987 回答
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

于 2020-01-30T11:14:05.257 回答
0

你可以使用这个:

<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>
于 2019-09-12T12:14:28.853 回答