嵌套片段不符合最大宽度

时间:2018-12-14 20:12:42

标签: android android-fragments kotlin

我正在尝试迁移到单一活动应用程序(并准备使用新的JetPack导航库)。

我得到了一个灵活的UI(典型的双窗格片段):具有与大小相关的布局的活动,并动态添加一个或两个片段(如here所述)。主片段包含项目列表,次片段包含项目详细信息。

我想将Activity中包含的逻辑转换为Fragment,并且一切工作正常,但是由于某种原因,在宽布局的情况下(可以同时显示主片段和辅助片段的布局) (例如,在平板电脑中)主要片段占据整个宽度(应为50%)。次要片段效果很好(可以在主要片段下面看到)。

Bad layout

有什么想法可以解决此问题吗?

活动布局:

<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版式(我尝试了很多不同的版式,但是都行不通,活动字中的代码相同):

<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%)?

1 个答案:

答案 0 :(得分:0)

尝试一下

<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" />