[英]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.