android - 嵌套片段不考虑最大宽度
问题描述
我正在尝试迁移到单活动应用程序(并准备使用新的 JetPack 导航库)。
我得到了一个灵活的 UI(典型的双窗格片段):具有大小相关布局并动态添加一个或两个片段的 Activity(如此处所述)。主要片段包含项目列表,次要片段包含项目详细信息。
我想将 Activity 中包含的逻辑转换为 Fragment 并且一切正常,但由于某种原因,在宽布局的情况下(允许同时显示主要片段和次要片段的布局,在片剂,例如)主要片段占据整个宽度(应该是50%)。次要片段效果很好(可以在主要片段下方看到)。
有什么想法可以解决这个问题吗?
活动布局:
<fragment
android:id="@+id/mainFragment"
android:name="jp.rodriguez.kanjisenpai.fragment.StudyListManagePanelFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"/>
StudyListManagePanelFragment 布局创建:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_studylistmanage, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Make back button work properly in single pane version
fragmentManager!!.beginTransaction().setPrimaryNavigationFragment(this).commit()
if (savedInstanceState == null) {
studyingFragment = StudyListManageFragment()
childFragmentManager
.beginTransaction()
.setPrimaryNavigationFragment(studyingFragment)
.add(R.id.mainFragment, studyingFragment)
.commit()
} else
studyingFragment = childFragmentManager.findFragmentById(R.id.mainFragment) as StudyListManageFragment
}
fun showStudyListDetails(studyList: StudyList) {
if (view!!.findViewById<View>(R.id.fragmentDetails) != null) { // Dual pane
val oldDetailsFragment = termsFragment
val fragmentTransaction = childFragmentManager.beginTransaction()
if (oldDetailsFragment != null)
fragmentTransaction.remove(oldDetailsFragment)
if (studyList.itemCount != 0 || studyList.isUserCreated) {
val detailsFragment = StudyListTermFragment()
detailsFragment.studyListId = studyList.id
fragmentTransaction
.add(R.id.fragmentDetails, detailsFragment, "termDetailsFragment")
}
fragmentTransaction.commit()
}
}
xlarge 布局(我尝试了很多不同的布局,但它不起作用,Activity 单词中的代码相同):
<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">
<FrameLayout
android:id="@+id/mainFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".5"
tools:layout="@layout/list_item_studylist" />
<FrameLayout
android:id="@+id/fragmentDetails"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".5"
tools:ignore="InconsistentLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
两个片段都是 androidx.fragment.app.ListFragment。
任何想法为什么第一个 FrameLayout 在与嵌套片段一起使用时总是具有最大宽度(不是 50%)?
解决方案
尝试这个
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
<FrameLayout
android:id="@+id/mainFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline"
tools:layout="@layout/list_item_studylist" />
<FrameLayout
android:id="@+id/fragmentDetails"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/guideline"
tools:ignore="InconsistentLayout" />
推荐阅读
- assembly - 程序集 x86 值未正确复制?
- javascript - UTF8 解码汉字
- json - spring/jackson-databind 小升级后 HttpMessageConverter 不再工作
- wtforms - 'TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given'
- amazon-web-services - 一个或多个子网的路由表中可以有两个 NAT 网关吗?
- php - include custom code in wordpress for purge varnish
- python - calculation for every cell in row pandas
- c# - 为什么正则表达式组提取给出错误的答案?
- typescript - Is there a way to get the keys of an interface where interface[key] is a type?
- c++ - 多态性和在 C++ 中定义不同对象的列表