【发布时间】:2018-12-14 20:12:42
【问题描述】:
我正在尝试迁移到单活动应用程序(并准备使用新的 JetPack 导航库)。
我得到了一个灵活的 UI(典型的双窗格片段):具有大小相关布局并动态添加一个或两个片段的活动(如 here 所述)。主要片段包含项目列表,次要片段包含项目详细信息。
我想将 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 android-fragments kotlin