如何解决Android Kotlin:无法使用约束布局在警报对话框中加载Webview
我的资产目录中有一个非常简单的html页面,无法使用带有约束布局的警报对话框来加载它。如果我改为使用线性布局,则相同的代码可以正常工作。示例代码首先在onResume中加载html,因此我可以看到webview在非对话框约束视图中的约束条件下工作。它包含一个用于调用对话框代码的按钮。 onResume可以正常工作,但是显示对话框时,我得到的只是一个空白对话框。
我的主要活动是:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun Activity.showDialog() {
val builder = androidx.appcompat.app.AlertDialog.Builder(this)
val view = layoutInflater.inflate(R.layout.info_dialog,null)
val webview = view.findViewById<WebView>(R.id.info_WV)
webview.loadUrl("file:///android_asset/www/Privacy.html")
builder.setView(view)
builder.show()
}
override fun onResume() {
val webview = findViewById<WebView>(R.id.webview)
webview.loadUrl("file:///android_asset/www/Privacy.html")
val button = findViewById<Button>(R.id.button)
button.setOnClickListener({_ -> showDialog() })
super.onResume()
}
}
活动布局如下:
<?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"
>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/button"
>
</WebView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_alert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
对话框布局如下:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="@+id/info_WV"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
我已经在约束布局离子警报对话框中查看了其他几个问题,但似乎都不合适。警报对话框中的约束布局有什么特别之处吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。