|
|
@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
package net.micode.notes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.activity.EdgeToEdge;
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
|
|
import androidx.core.graphics.Insets;
|
|
|
|
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
|
|
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 应用的主活动类,负责设置全屏沉浸体验并处理窗口安全区域
|
|
|
|
|
|
|
|
* 通过使用 EdgeToEdge API 和 WindowInsets 实现状态栏和导航栏的透明效果
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 启用边缘到边缘显示模式(全屏沉浸效果)
|
|
|
|
|
|
|
|
// 这会使内容延伸到状态栏和导航栏下方
|
|
|
|
|
|
|
|
EdgeToEdge.enable(this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置活动的布局文件
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 为主要内容视图设置窗口安全区域的处理逻辑
|
|
|
|
|
|
|
|
// 确保内容不会被状态栏、导航栏或其他系统UI元素遮挡
|
|
|
|
|
|
|
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
|
|
|
|
|
|
|
|
// 获取系统栏(状态栏和导航栏)的安全区域内边距
|
|
|
|
|
|
|
|
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 为视图应用内边距,确保内容在安全区域内显示
|
|
|
|
|
|
|
|
// 这样可以避免内容被系统UI遮挡,同时保持全屏沉浸的视觉效果
|
|
|
|
|
|
|
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 返回处理后的窗口插入,传递给后续的处理程序
|
|
|
|
|
|
|
|
return insets;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|