代码实现
1、准备工作
设置,我们需要在config.json文件上添加如下权限,代码如下
“reqPermissions”: [
{“name”: “ohos.permission.SYSTEM_FLOAT_WINDOW”}
],
2、绘画Xml布局
我们MianAbilitySlice的xml的布局,一个文本悬浮框,代码变成
[DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical"](DirectionalLayout
xmlns:ohos=)
[Text
ohos:id="$ id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="显示悬浮框"
ohos:text_size="40vp"
/](Text
ohos:id=)
下面的代码在显示我们一个悬浮框的xml布局,精心显示浮动的xml,然后在_window中的文字,为组件用于悬浮的
[DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:background_element="#20000000"
ohos:orientation="vertical"](DirectionalLayout
xmlns:ohos=)
[Text
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="horizontal_center"
ohos:text="这个是一个悬浮框"
ohos:text_size="40vp"
/](Text
ohos:height=)
3、java代码实现
文字实现点击组件的悬浮事件,判断是否存在应用层显示权限,如果存在的话框,如果不存在的话关闭界面,代码如下
package com.aispeech.mileohos.slice;
import com.aispeech.mileohos.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.sysappcomponents.settings.AppSettings;
import ohos.utils.net.Uri;
public class MainAbilitySlice extends AbilitySlice {
private Window window;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//todo 设置点击事件
findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component view) {
//todo 判断是否有在应用界面上显示的权限
if (AppSettings.canShowFloating(MainAbilitySlice.this)) {
//todo 如果存在的话 显示悬浮框
show();
} else {
//todo 如果不存在的话 添加设置界面进行设置
Intent intent2 = new Intent();
intent2.setAction("android.settings.action.MANAGE_OVERLAY_PERMISSION");
Uri parse = Uri.parse("package:" getBundleName());
intent2.setUri(parse);
startAbilityForResult(intent2, 1000);
}
}
});
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
boolean b = AppSettings.canShowFloating(this);
System.out.println("songy 不给授权" b);
if (requestCode == 1000) {
if (AppSettings.canShowFloating(this)) {
show();
}else {
// todo 没授权 关闭界面
terminateAbility();
}
}
}
/**
-
悬浮框的实现
*/
private void show(){
//todo 加载悬浮框的布局
ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(MainAbilitySlice.this).parse(ResourceTable.Layout_float_window,null,false);
window=WindowManager.getInstance().
addComponent(componentContainer,MainAbilitySlice.this,WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
window.setTransparent(true);
window.setMovable(true);
WindowManager.LayoutConfig layoutConfig=new WindowManager.LayoutConfig();
//todo layoutConfig属性设置
layoutConfig.width=900;
layoutConfig.height=900;
int flags = WindowManager.LayoutConfig.MARK_TOUCH_MODAL_IMPOSSIBLE |
WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE |
WindowManager.LayoutConfig.MARK_ALLOW_LAYOUT_COVER_SCREEN;
layoutConfig.flags = flags;
window.setLayoutConfig(layoutConfig);
}
}
运行效果
原作者:Mayism