gpt4 book ai didi

Android - 覆盖屏幕包括状态栏内容

转载 作者:太空狗 更新时间:2023-10-29 13:54:23 24 4
gpt4 key购买 nike

我想制作一个覆盖整个屏幕的覆盖屏幕,包括状态栏和导航栏。

我花了几个小时谷歌搜索/研究仍然没有运气。

这是我的代码:

MainActivity.java(启动器):

package com.blogspot.diannaoxiaobai.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startActivity(new Intent(this, Main2Activity.class));
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blogspot.diannaoxiaobai.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

Main2Activity.java(由 MainActivity 启动):

package com.blogspot.diannaoxiaobai.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class Main2Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
setContentView(R.layout.activity_main2);
}
}

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:background="#99ed3636"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.blogspot.diannaoxiaobai.myapplication.Main2Activity">
<LinearLayout android:id="@+id/overlay_instruction"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/transparent"
android:clickable="true">

</LinearLayout>
</LinearLayout>

AndroidManifest.xml(仅添加了android:theme="@style/Theme.Transparent",其余自动生成):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.diannaoxiaobai.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:theme="@style/Theme.Transparent"></activity>
</application>

</manifest>

样式.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="Theme.Transparent" parent="android:Theme.Holo.NoActionBar.Fullscreen">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="colorPrimary">@android:color/transparent</item>
<item name="colorPrimaryDark">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFullscreen">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>

</resources>

启动时的截图,确实成功覆盖了整个屏幕,包括状态栏和导航栏,但是状态栏内容(例如时钟,电池状态)不见了:

enter image description here

当我按回键关闭 Overlay Activity 时,状态栏内容又回来了:

enter image description here

我想要覆盖状态栏而不删除内容,我应该怎么做?

最佳答案

自从你发帖后我就试过了,并想到了这个,

在 Manifest.xml 中 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

 public class MainActivity extends AppCompatActivity {


private WindowManager wm;
private ViewGroup mTopView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


applyOverLay();


mTopView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set your status bar visible Here
Toast.makeText(MainActivity.this, "Ding Ding ,I got a Click Removing overlay!!", Toast.LENGTH_SHORT).show();
wm.removeViewImmediate(mTopView);

}
});

}


public void applyOverLay() {

//set your status bar gone Here
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);

wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.inflator, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
}


}

充气机.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="25dp"
android:background="#56FF0000"
>
</LinearLayout>

功能正常,根据您的要求进行调整。将代码添加到ViewGone我评论的地方的状态栏。

编辑:你不需要这个,但正如你所说你得到的是空指针我也添加了这个。它对你没有用,只是为了避免错误,这是你的activity_main不是我的。现在,如果您无法修复它,请将其添加为您的 activity_main也是。(根据需要更改它)

activity_main.XML

<LinearLayout
android:id="@+id/lin"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:background="#65000000"
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="200dp" />

<ImageView
android:background="#739"
android:layout_width="match_parent"
android:layout_height="200dp" />

<ImageView
android:background="#789234"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>

享受

输出:

enter image description here enter image description here

关于Android - 覆盖屏幕包括状态栏内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com