问题描述
我的资产目录中有一个非常简单的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 (将#修改为@)