Android 使用 Jetpack Compose 覆盖 window
Android overlay window using jetpack compose
我需要开发一个 android 库,它应该能够允许正在使用我的库的开发人员在他当前屏幕的顶部显示一个覆盖层 window。我可以为此使用弹出式覆盖,但覆盖 UI 应该是声明性的。我正在尝试使用 Jetpack compose 为覆盖创建声明性 UI。
我在 kotlin 文件中创建了一个简单的弹出窗口,如下所示
class MyView {
@Composable
fun pop() {
Box {
val popupWidth = 200.dp
val popupHeight = 50.dp
val cornerSize = 16.dp
Popup(alignment = Alignment.Center) {
// Draw a rectangle shape with rounded corners inside the popup
Box(
Modifier
.size(popupWidth, popupHeight)
.background(Color.White, RoundedCornerShape(cornerSize))
)
}
}
}
}
为了测试这个,我创建了一个 activity 按钮,如下所示
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
@ExperimentalMaterialApi
fun showComposeView(view: android.view.View)
{
setContent {
MyView().pop()
}
}
}
上面activity的布局文件如下,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="Button"
android:onClick="showComposeView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
现在,问题是,当我单击按钮时,上面布局文件中的视图被 compose 弹出窗口所取代。
我想编写弹出窗口以显示在现有布局视图上。
如何实现?
调用 setContent{}
实际上就像调用 setContentView
,您替换 Activity
的根 View
。
您想要做的是混合 Compose
和常规 View
,您最好的选择可能是将您的布局放在 AndroidView
可组合项中。
您可以找到更多信息 here
@Composable
fun MyPopupContainer(@LayoutRes layout: Int) {
var showPopup by remember { mutableStateOf(false) }
AndroidView(factory = { context ->
// Inflate your view the way you want, using layout inflater or ViewBinding
val view = LayoutInflater.from(context).inflate(layout, null)
view.findViewById<View>(R.id.button).setOnClickListener {
showPopup = true
}
// returning the view we just created
view
})
// When showPopup is false, your popup won't be displayed
if (showPopup) {
// Your popup function here
MyView().pop()
}
}
然后在你的 Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { MyPopupContainer(R.layout.your_xml_layout) }
}
我可以使用底部 sheet 对话框片段在现有布局上显示撰写弹出窗口。
我需要开发一个 android 库,它应该能够允许正在使用我的库的开发人员在他当前屏幕的顶部显示一个覆盖层 window。我可以为此使用弹出式覆盖,但覆盖 UI 应该是声明性的。我正在尝试使用 Jetpack compose 为覆盖创建声明性 UI。 我在 kotlin 文件中创建了一个简单的弹出窗口,如下所示
class MyView {
@Composable
fun pop() {
Box {
val popupWidth = 200.dp
val popupHeight = 50.dp
val cornerSize = 16.dp
Popup(alignment = Alignment.Center) {
// Draw a rectangle shape with rounded corners inside the popup
Box(
Modifier
.size(popupWidth, popupHeight)
.background(Color.White, RoundedCornerShape(cornerSize))
)
}
}
}
}
为了测试这个,我创建了一个 activity 按钮,如下所示
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
@ExperimentalMaterialApi
fun showComposeView(view: android.view.View)
{
setContent {
MyView().pop()
}
}
}
上面activity的布局文件如下,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="Button"
android:onClick="showComposeView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
现在,问题是,当我单击按钮时,上面布局文件中的视图被 compose 弹出窗口所取代。
我想编写弹出窗口以显示在现有布局视图上。
如何实现?
调用 setContent{}
实际上就像调用 setContentView
,您替换 Activity
的根 View
。
您想要做的是混合 Compose
和常规 View
,您最好的选择可能是将您的布局放在 AndroidView
可组合项中。
您可以找到更多信息 here
@Composable
fun MyPopupContainer(@LayoutRes layout: Int) {
var showPopup by remember { mutableStateOf(false) }
AndroidView(factory = { context ->
// Inflate your view the way you want, using layout inflater or ViewBinding
val view = LayoutInflater.from(context).inflate(layout, null)
view.findViewById<View>(R.id.button).setOnClickListener {
showPopup = true
}
// returning the view we just created
view
})
// When showPopup is false, your popup won't be displayed
if (showPopup) {
// Your popup function here
MyView().pop()
}
}
然后在你的 Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { MyPopupContainer(R.layout.your_xml_layout) }
}
我可以使用底部 sheet 对话框片段在现有布局上显示撰写弹出窗口。