笔记3 | 通过onWindowAttributesChanged和onSystemUiVisibilityChange监听状态栏页面的隐藏与显示、动态显示与隐藏状态栏

地址

CSDN 地址:http://blog.csdn.net/xiangyong_1521/article/details/51333437

工作中的一个需求,需要监控并动态控制到SystemUi(状态栏)的显示与隐藏,这是该需求的整理;

目录

  • onWindowAttributesChanged方法

  • OnSystemUiVisibilityChangeListene方法

  • setSystemUiVisibility(int visibility)控制

一、onWindowAttributesChanged方法监听

@Override  
ublic void onWindowAttributesChanged(LayoutParams params) {  
super.onWindowAttributesChanged(params);  
Log.e("Even","屏幕状态"+getWindow().getAttributes().fl
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是基于安卓的点击状态栏图标显示笔记标签的代码: 首先,在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 接着,在 MainActivity.java 文件中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE_NOTE_TAG = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建状态栏通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_note) .setContentTitle("笔记标签") .setContentText("点击查看笔记标签") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true); Intent intent = new Intent(this, NoteTagActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_NOTE_TAG, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1, builder.build()); } } ``` 上述代码中,我们创建了一个状态栏通知,当用户点击通知时,会启动 NoteTagActivity。 接着,在 NoteTagActivity.java 文件中添加以下代码: ```java public class NoteTagActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note_tag); // 显示笔记标签 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.START; params.x = 0; params.y = 0; LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.layout_note_tag, null); TextView tvNoteTag = view.findViewById(R.id.tv_note_tag); tvNoteTag.setText("这是一个笔记标签"); WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); windowManager.addView(view, params); } @Override protected void onDestroy() { super.onDestroy(); // 移除笔记标签 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); View view = getWindow().getDecorView(); windowManager.removeView(view); } } ``` 上述代码中,我们创建了一个 NoteTagActivity,用于显示笔记标签。在 onCreate() 方法中,我们创建了一个悬浮窗口,并将笔记标签添加到悬浮窗口中。在 onDestroy() 方法中,我们移除悬浮窗口。 最后,在 layout_note_tag.xml 文件中添加以下代码: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_note_tag" android:padding="8dp"> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/ic_note" android:tint="@color/colorWhite" android:contentDescription="笔记图标"/> <TextView android:id="@+id/tv_note_tag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorWhite" android:text="笔记标签"/> </LinearLayout> ``` 上述代码中,我们创建了一个笔记标签的布局,包含一个图标和一个文本。我们使用 bg_note_tag.xml 文件作为背景,使笔记标签看起来更加美观。 以上就是基于安卓的点击状态栏图标显示笔记标签的代码。运行程序后,当用户点击状态栏图标时,会显示一个笔记标签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值