[英]Transparent background in custom overlay view not working
我正在尝试重新创建 Android PopupWindow 或 System AlertDialog 的外观和感觉,其中视图浮动在所有应用程序之上; 我设法显示视图,但没有用圆形透明角显示它:
视图布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="4dp"
app:elevation="8dp">
<TextView
android:background="@drawable/card_background"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
tools:text="Hallo" />
</FrameLayout>
视图背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1px" android:color="#aaa"/>
<corners android:radius="8dp"/>
<solid android:color="#fff"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
和视图管理器代码:
val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
currentView.text.setText(text)
currentView.setBackgroundColor(Color.WHITE)
val makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
currentView.measure(makeMeasureSpec, makeMeasureSpec)
currentView.layout(0, 0, currentView.measuredWidth, currentView.measuredHeight)
val params = WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
PixelFormat.TRANSLUCENT)
params.gravity = Gravity.START or Gravity.TOP
params.x = location.left.toInt()
params.y = location.bottom.toInt()
if (!currentView.isAttachedToWindow) {
wm.addView(currentView, params)
} else {
wm.updateViewLayout(currentView, params)
}
我尝试了一些与 CardView 等其他视图相关的解决方案,但没有成功。 所以我怀疑这个问题一定与 WindowManager 及其可绘制的窗口背景有关。
也许,您可能希望将<solid>
颜色更改为#50FFFFFF
,其中前 2 位数字值表示不透明度值。
并且还将您的片段背景更改为您自己的背景。
卡片背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1px" android:color="#aaa"/>
<corners android:radius="8dp"/>
<solid android:color="#50ffffff"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
xml文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/card_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="4dp"
app:elevation="8dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
tools:text="Hallo" />
</FrameLayout>
一夜好眠后,我找到了解决问题的方法:
// Set the background color of the top layer layout explicity
currentView.setBackgroundColor(Color.TRANSPARENT)
...
if (!currentView.isAttachedToWindow) {
wm.addView(currentView, params)
} else {
wm.updateViewLayout(currentView, params)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.