美文网首页
Android webview设置背景透明,去掉白色

Android webview设置背景透明,去掉白色

作者: 沐风雨木 | 来源:发表于2023-12-14 11:52 被阅读0次

最近根据项目特点,写了一个通用承载 webviewactivity,通过路由跳转,特点是 webview 半屏,类似于一个 dialog,代码如下:

  1. 首先设置 activity 透明:
 <activity
            android:name=".ui.dialog.ShareDialogActivity"
            android:configChanges="keyboardHidden|orientation"
            android:exported="false"
            android:theme="@style/DialogTheme" />

可以看到加了一个 theme

  <style name="DialogTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transparent</item>

        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowCloseOnTouchOutside">false</item>
    </style>
  1. 控件 webview
  <WebView
            android:id="@+id/ww_share_dialog"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

代码设置背景和透明度(主要看这里):

        // 设置背景色
        binding.wwShareDialog.setBackgroundColor(0);
        // 设置透明度 范围:0-255
        binding.wwShareDialog.getBackground().setAlpha(0);

如上,打开这个 activitywebview 未渲染的地方就是透明的,当然前端页面需要也是透明的。

相关文章

网友评论

      本文标题:Android webview设置背景透明,去掉白色

      本文链接:https://www.haomeiwen.com/subject/bcjmgdtx.html