Android XML 中的顶级布局总是 FrameLayout?
Top-level Layout in Android XML is ALWAYS FrameLayout?
我试图以编程方式设置 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
}
}
但我现在很困惑。有人帮忙吗?并提前谢谢你。 :)
如果您只设置高度,那么您可以使用更通用的 ViewGroup.LayoutParams
。所有布局参数都在扩展它,所以 class 没有机会进行转换,无论父级如何
val params = homeBottomSheetContainer.layoutParams as ViewGroup.LayoutParams
除此之外,您还可以使用 is
关键字来检查实例类型
我试图以编程方式设置 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
}
}
但我现在很困惑。有人帮忙吗?并提前谢谢你。 :)
如果您只设置高度,那么您可以使用更通用的 ViewGroup.LayoutParams
。所有布局参数都在扩展它,所以 class 没有机会进行转换,无论父级如何
val params = homeBottomSheetContainer.layoutParams as ViewGroup.LayoutParams
除此之外,您还可以使用 is
关键字来检查实例类型