I'm having to migrate to ViewBinding in my Android app to comply with the latest target versions. I'm really struggling to get a dialog box created using CardView to display correctly.
我不得不在我的Android应用程序中迁移到ViewBinding,以符合最新的目标版本。我真的很难让一个用CardView创建的对话框正确显示。
The below is the script after I've made the modifications to use ViewBinding.
下面是我进行修改以使用视图绑定后的脚本。
override fun addUserDialog() {
Handler(Looper.getMainLooper()).post(Runnable {
LoggerUtils.logMessage("PMN Calling MFA User Dialog")
var dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(bindingMFA.root)
dialog.setCancelable(false)
bindingMFA.mBtnCancel.setOnClickListener {
dialog.dismiss()
}
bindingMFA.mBtnConfirm.setOnClickListener {
mMFACode = bindingMFA.mSMSMFA.text.toString()
mLoginViewPresenter.confirmLogin(mMFACode)
dialog.dismiss()
}
dialog.show()
})
}
This appears within a file 'loginActivity.kt', where I set the binding within onCreate:
它出现在文件‘loginActivity.kt’中,我在该文件中设置了onCreate中的绑定:
private lateinit var binding: ActivityLoginBinding
private lateinit var bindingMFA: DialogMfaBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
bindingMFA = DialogMfaBinding.inflate(layoutInflater)
If I change the line back to dialog.setContentView(R.layout.dialog_mfa)
it shows the dialog box absolutely fine.
如果我将该行改回Dialog.setContent View(R.layout.Dialog_MFA),则会完全正确地显示该对话框。
This is the dialog_mfa.xml file:
这是DIALOG_mfa.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="300dp"
android:layout_height="wrap_content" app:cardCornerRadius="4dp"
app:cardElevation="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_margin="4dp">
<TextView
android:id="@+id/mLblInvitationHeader"
style="@style/LabelTextMediumBold"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="@color/background_blue"
android:gravity="center"
android:text="@string/mfa_header"
android:textColor="@color/text_white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/mLblHeader"
style="@style/LabelTextMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:singleLine="false"
android:text="@string/mfa_intro"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mLblInvitationHeader" />
<EditText
android:id="@+id/mSMS_MFA"
style="@style/LabelTextMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mLblHeader" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mBtnCancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="@+id/mBtnConfirm"
android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="@+id/mSMS_MFA"
android:layout_marginStart="8dp" android:layout_marginEnd="4dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" style="@style/GeneralButtonGrey"
android:text="@string/cancel"/>
<Button
android:text="@string/mfa_confirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mBtnConfirm"
app:layout_constraintStart_toEndOf="@+id/mBtnCancel" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/mSMS_MFA" android:layout_marginEnd="8dp"
android:layout_marginStart="4dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" style="@style/GeneralButton"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
If anyone is able to point out what I'm doing wrong, it would be very much appreciated!
如果有人能指出我做错了什么,我将不胜感激!
更多回答
优秀答案推荐
Try the following:
尝试以下操作:
bindingMFA = DialogMfaBinding.inflate(layoutInflater)
bindingMFA.window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
try adding android_layout_weight=1 to the textViews
尝试将Android_Layout_Weight=1添加到文本视图
更多回答
Thanks - that almost worked. Only change is that bindingMFA didn't have a window attribute, but this worked dialog.window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
谢谢--这招差一点就成功了。唯一的变化是bindingMFA没有窗口属性,但这可以工作dialog.window?.setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
我是一名优秀的程序员,十分优秀!