我試圖以編程方式設定 ConstraintLayout 的高度,我打算使用ConstraintLayout.LayoutParams. ConstraintLayout 是此 XML 中的頂級布局。但是當我將高度從舊到新設定ConstraintLayout.LayoutParams并
ClassCastException發生時,我有奇怪的經歷。
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to androidx.constraintlayout.widget.ConstraintLayout$LayoutParams
詳細代碼如下。
對話
private fun initLayoutSize() = with(binding) {
dialog?.let {
val height = getDisplayHeight(it.window!!.windowManager)
val params = homeBottomSheetContainer.layoutParams as ConstraintLayout.LayoutParams // excpeiton here!!
params.height = (height * 0.3).toInt()
homeBottomSheetContainer.layoutParams = params
}
}
XML
<?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:id="@ id/homeBottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="@dimen/basic16"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<View
android:id="@ id/homeBottomSheetTopView"
android:layout_width="@dimen/basic30"
android:layout_height="@dimen/basic03"
android:layout_marginTop="@dimen/basic12"
android:background="@drawable/bottomsheet_top_radius_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/homeBottomLottoSearchNotice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/basic12"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="end"
android:padding="@dimen/basic04"
android:text="@string/ask_search"
android:textColor="@color/black"
android:textSize="@dimen/txt12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/homeBottomSheetTopView"
tools:visibility="invisible" />
<EditText
android:id="@ id/homeLottoSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/input_lotto_round"
android:imeOptions="actionDone"
android:inputType="number"
android:maxLines="1"
android:textColor="@color/black"
android:textCursorDrawable="@drawable/shape_edittext_cursor_black"
android:textSize="@dimen/txt12"
android:visibility="invisible"
app:backgroundTint="@color/black"
app:layout_constraintBottom_toBottomOf="@id/homeBottomLottoSearchNotice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/homeBottomLottoSearchNotice" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/homeBottomSheetLottoRV"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/basic12"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/homeBottomLottoSearchNotice"
tools:listitem="@layout/item_lotto_all_round" />
</androidx.constraintlayout.widget.ConstraintLayout>
發生了什么事?
我沒有在我的 XML 中使用任何 FrameLayout,我在任何地方都找不到它。但是為什么android系統告訴我有關FrameLayout的資訊?FrameLayout 是 Android View XML 中的默認頂級布局嗎???
所以,我用下面的代碼更改了代碼。它奏效了!
private fun initLayoutSize() = with(binding) {
dialog?.let {
val height = getDisplayHeight(it.window!!.windowManager)
val params = homeBottomSheetContainer.layoutParams as FrameLayout.LayoutParams // change here!!
params.height = (height * 0.3).toInt()
homeBottomSheetContainer.layoutParams = params
}
}
但我現在很困惑。有人幫忙嗎?并提前感謝您。:)
uj5u.com熱心網友回復:
如果您只設定高度,那么您可以使用更通用ViewGroup.LayoutParams的 . 所有布局引數都在擴展它,因此無論父級如何,都沒有機會進行類轉換
val params = homeBottomSheetContainer.layoutParams as ViewGroup.LayoutParams
除此之外,您可以使用is關鍵字來檢查實體型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422503.html
標籤:
上一篇:安卓。ConstraintLayout,如何應用goneMargin
下一篇:如何將廣告視圖與游戲視圖分開?
