带 DisplayCutout 的全屏应用程序
Fullscreen App With DisplayCutout
如何制作具有实际全屏功能的应用程序,其布局要在缺口下方呈现?
这是我想要的:
这是我试过的代码:
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
val attrib = window.attributes
attrib.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
layout_main.setOnApplyWindowInsetsListener { _, windowInsets ->
val inset = windowInsets.displayCutout
Log.d("Tag", "Inset: $inset")
windowInsets
}
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3000FFFF"
android:fitsSystemWindows="true">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF00FF00"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF0000FF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFF00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
风格:
<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>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
这是 Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.testandroidp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.app.testandroidp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我目前得到的结果:
我试过设置不同的主题,在 XML / Kotlin 中设置全屏标志,在清单中设置可调整大小的 activity,但是 activity 不会被渲染在缺口下。
供参考,这是项目的源代码:https://gitlab.com/alvin.rusli/AndroidPTest
尝试以下风格,
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
我终于找到原因了。由于某些奇怪的原因,应用程序不会进入 if 条件:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// It never gets here
}
我删除了 if 条件,activity 终于正确地进入了全屏。
这里是呈现 activity 全屏所需的最低限度代码。
Activity:
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>
那是因为你用的是 Android P,当时它还没有正式发布。您的 Developer Preview 可能与供应商有问题。如果您在 Google 中搜索您的系统并最终进入 XDA 页面,您可能会在 Android 版本的错误中找到 "Vendor"。通常,Developer Previews 保留以前 Android 版本的供应商,因此,当您在下面的行中检查它时,您的应用程序认为在 Android Oreo 中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
我在为 Notch 设备开发应用程序时遇到了同样的问题,我通过为 android P 设备定义主题样式并将模式设置为默认、shortEdges、从不模式来解决这个问题。
第 1 步:
在您的 res 目录中创建一个新的 values-v28 文件夹,并将默认的 styles.xml 文件复制到其中。
第 2 步:
在 styles.xml 文件的 values-28 变体中,转到您的 Activity 主题或创建一个新主题(如果您使用的是默认主题),然后设置此属性:
<style name="ActivityTheme">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges
</item>
</style>
这就是您在应用中获得更身临其境的体验并通过使应用的 window 在凹口的两侧区域呈现来避免信箱所要做的全部。
Here is the link to learn more about Developing Compatible App for Notch Devices.
Making Notch Friendly Apps
在您的主题中设置这些属性:
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
如果没有效果就试试这个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
我尝试了很多但没有成功,然后我尝试了下面的代码,它工作正常。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
---
}
希望对您有所帮助。
这对我有用:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
下面的代码对我有用,我希望它对你也有用:
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //show the activity in full screen
setContentView(R.layout.activity_pdf_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
如果您想在应用中的任何地方应用它,那么根据 Android 文档有一个更简单的解决方案:
将此项添加到您的主题文件中
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, or never -->
</item>
如何制作具有实际全屏功能的应用程序,其布局要在缺口下方呈现?
这是我想要的:
这是我试过的代码:
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
val attrib = window.attributes
attrib.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
layout_main.setOnApplyWindowInsetsListener { _, windowInsets ->
val inset = windowInsets.displayCutout
Log.d("Tag", "Inset: $inset")
windowInsets
}
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3000FFFF"
android:fitsSystemWindows="true">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF00FF00"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF0000FF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFF00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
风格:
<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>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
这是 Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.testandroidp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.app.testandroidp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我目前得到的结果:
我试过设置不同的主题,在 XML / Kotlin 中设置全屏标志,在清单中设置可调整大小的 activity,但是 activity 不会被渲染在缺口下。
供参考,这是项目的源代码:https://gitlab.com/alvin.rusli/AndroidPTest
尝试以下风格,
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
我终于找到原因了。由于某些奇怪的原因,应用程序不会进入 if 条件:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// It never gets here
}
我删除了 if 条件,activity 终于正确地进入了全屏。
这里是呈现 activity 全屏所需的最低限度代码。
Activity:
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>
那是因为你用的是 Android P,当时它还没有正式发布。您的 Developer Preview 可能与供应商有问题。如果您在 Google 中搜索您的系统并最终进入 XDA 页面,您可能会在 Android 版本的错误中找到 "Vendor"。通常,Developer Previews 保留以前 Android 版本的供应商,因此,当您在下面的行中检查它时,您的应用程序认为在 Android Oreo 中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
我在为 Notch 设备开发应用程序时遇到了同样的问题,我通过为 android P 设备定义主题样式并将模式设置为默认、shortEdges、从不模式来解决这个问题。
第 1 步: 在您的 res 目录中创建一个新的 values-v28 文件夹,并将默认的 styles.xml 文件复制到其中。
第 2 步: 在 styles.xml 文件的 values-28 变体中,转到您的 Activity 主题或创建一个新主题(如果您使用的是默认主题),然后设置此属性:
<style name="ActivityTheme">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges
</item>
</style>
这就是您在应用中获得更身临其境的体验并通过使应用的 window 在凹口的两侧区域呈现来避免信箱所要做的全部。
Here is the link to learn more about Developing Compatible App for Notch Devices. Making Notch Friendly Apps
在您的主题中设置这些属性:
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
如果没有效果就试试这个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
我尝试了很多但没有成功,然后我尝试了下面的代码,它工作正常。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
---
}
希望对您有所帮助。
这对我有用:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
下面的代码对我有用,我希望它对你也有用:
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //show the activity in full screen
setContentView(R.layout.activity_pdf_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
如果您想在应用中的任何地方应用它,那么根据 Android 文档有一个更简单的解决方案:
将此项添加到您的主题文件中
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, or never -->
</item>