【Android】Build Non-Fullscreen Transparent Activity

Intent

build an activity looks like a dialog, float over main activity or desktop

Create Transparent Theme
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.TransparentActivity" parent="Theme.Material3.Light.NoActionBar">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
    </style>
</resources>
Apply Transparent Theme to Activity
xml 复制代码
<activity
    android:name=".viewlocator.DialogActivity"
    android:theme="@style/Theme.TransparentActivity" />
Make Activity Non-Fullscreen
kotlin 复制代码
class DialogActivity : AppCompatActivity() {

    private lateinit var binding: ActivityHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
  
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            val lp = window.attributes
            lp.width = 500
            lp.height = 500
            lp.gravity = Gravity.CENTER
            window.attributes = lp
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
    }
}
相关推荐
molong93110 分钟前
Android 应用配置跳转微信小程序
android·微信小程序·小程序
2501_9151063220 分钟前
iOS 可分发是已经上架了吗?深入解析应用分发状态、ipa 文件上传、TestFlight 测试与 App Store 审核流程
android·ios·小程序·https·uni-app·iphone·webview
安东尼肉店9 小时前
Android compose屏幕适配终极解决方案
android
2501_916007479 小时前
HTTPS 抓包乱码怎么办?原因剖析、排查步骤与实战工具对策(HTTPS 抓包乱码、gzipbrotli、TLS 解密、iOS 抓包)
android·ios·小程序·https·uni-app·iphone·webview
feiyangqingyun10 小时前
基于Qt和FFmpeg的安卓监控模拟器/手机摄像头模拟成onvif和28181设备
android·qt·ffmpeg
用户20187928316714 小时前
ANR之RenderThread不可中断睡眠state=D
android
煤球王子14 小时前
简单学:Android14中的Bluetooth—PBAP下载
android
小趴菜822714 小时前
安卓接入Max广告源
android
齊家治國平天下14 小时前
Android 14 系统 ANR (Application Not Responding) 深度分析与解决指南
android·anr
ZHANG13HAO14 小时前
Android 13.0 Framework 实现应用通知使用权默认开启的技术指南
android