布局小记

本文介绍了如何在Service中创建Dialog,并详细讲述了如何将布局转换为具有20px圆角的背景,包括在布局文件中添加自定义背景以及创建白色且带圆角的背景设置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Service中创建Dialog 

    public void createWindow(){
        Dialog dialog = new Dialog(getApplicationContext());
        Window window = dialog.getWindow();
        //service中添加window使用该类型,
        //并需要声明权限:
        //<uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY"/>
        //<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        window.setGravity(Gravity.LEFT|Gravity.TOP);
        //设置Dialog背景
        window.setBackgroundDrawableResource(R.color.white);

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.x = 0;
        layoutParams.y = 500;
        //layoutParams.width = 450;
        //layoutParams.height = 300;
        window.setAttributes(layoutParams);

        //去除title
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        window.setContentView(R.layout.activity_main);
        dialog.show();
    }

将布局变为圆角

1.在布局中添加自己的background

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/layout_border"
    >

    <TextView
        android:text="这是一个TextView"
        android:layout_width="450px"
        android:layout_height="300px"/>


</LinearLayout>

2.创建background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="@android:color/white" />
    <corners android:radius="20px" />

</shape>

 设置Background 为白色,圆角为20px

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值