android - ConstraintLayout 的 ViewStub 膨胀
问题描述
我想利用ViewStub来选择性地显示 UI 的一部分。当我的膨胀项目的根是ConstraintLayout时,它不会呈现。
但是,如果该根是MaterialCardView,那么它会按预期显示。
<!-- my_fragment.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"
android:id="@+id/fragmentRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="@+id/fragmentViewStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/fragmentViewStub"
android:layout="@layout/item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- item.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"
android:id="@+id/itemRootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/itemLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/itemLabelText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/* MyFragment.kt */
@AndroidEntryPoint
class MyFragment : Fragment(R.layout.my_fragment) {
private val viewModel: MyViewModel by viewModels()
private val viewBinding: MyFragmentBinding by viewLifecycleLazy {
MyFragmentBinding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding.fragmentViewStub.inflate()
}
}
我尝试匹配 ViewStub 的id
/inflatedId
属性并以编程方式设置尺寸/约束,但都没有解决问题。
我忽略了让这些作品一起工作的原因是什么?
解决方案
忘记更新我的答案/监督。这在这两种情况下一直按预期工作。
启用了夜间模式,MaterialCardView
并且其卡片和内容之间的行为具有自动对比。同时,当ConstraintLayout
反转其主题时,标签似乎丢失但确实存在,作为白色背景上的白色文本。
推荐阅读
- cordova - 由于最近删除了另一个插件,因此无法删除 cordova 插件
- python-sphinx - 在Sphinx中使用graphviz的图表中的内部引用?
- sql - 如何从多个数据库(在多个服务器中)的多个表中找到重复的“ID”?
- c++ - 无法正确读取导出目录
- java - Java Quarkus Panache (Hibernate) 不更新实体属性
- python - openpyxl 如何改变“所有”单元格的大小?
- javascript - 提交功能后使用jquery下载pdf
- r - 尝试使用 knitr::knit('slides.Rmd') 编织 ioslides 无法在 html 中正确呈现
- telegram - Telegram bot /getUpdates 不包含数据
- amazon-web-services - AWS API Gateway 403 Forbidden with or without Cognito User Pool