android 片段大小与TabLayout重叠

eoxn13cs  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(119)

我发现了一些类似的问题,但没有一个能解决我的问题。但基本上和其他线程上的情况完全一样,我的viewPager片段与tabLayout重叠:

下面是我的源代码:

群组图书片段

public class GroupBookFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_group_book, container, false);

        // Tab layout initialization
        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
        Adapter_groupBook adapterGroupBook = new Adapter_groupBook(getParentFragmentManager(), getLifecycle());
        viewPager.setAdapter(adapterGroupBook);

        // set 3 Tab titles
        tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
        tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
        tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.selectTab(tabLayout.getTabAt(position));
            }
        });
        return view;
    }
}

片段_群组图书.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorAnimationMode="elastic" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager_groupBook"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

片段组图书脚本

public class fragment_groupBook_krippe extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_group_book_krippe, container, false);
    }
}

片段群组图书_krippe.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    tools:context=".fragment_groupBook_krippe">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.cardview.widget.CardView
            android:id="@+id/groupBook_krippe_1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:cardCornerRadius="30dp"
            app:cardElevation="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@id/groupBook_krippe_2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_weight="1">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="20dp">
                <TableRow
                    android:id="@+id/newsCard"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="20dp">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:contentDescription="@string/newsTitle"
                        android:src="@drawable/ic_baseline_mail_24"
                        app:tint="@color/creme_700" />
                    <TextView
                        android:id="@+id/newsTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/students_regular"
                        android:text="@string/newsTitle"
                        android:textAllCaps="true"
                        android:textSize="28sp"
                        android:textStyle="bold" />
                </TableRow>
            </TableLayout>
        </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:id="@+id/groupBook_krippe_2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:cardCornerRadius="30dp"
            app:cardElevation="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/groupBook_krippe_1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_weight="1">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="20dp">
                <TableRow
                    android:id="@+id/presenceCard"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="20dp">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:contentDescription="@string/presenceTitle"
                        android:src="@drawable/ic_baseline_accessibility_new_24"
                        app:tint="@color/creme_700" />
                    <TextView
                        android:id="@+id/presenceTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/students_regular"
                        android:text="@string/presenceTitle"
                        android:textAllCaps="true"
                        android:textSize="28sp"
                        android:textStyle="bold" />
                </TableRow>
            </TableLayout>
        </androidx.cardview.widget.CardView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>

适配器组通讯簿

public class Adapter_groupBook extends FragmentStateAdapter {
    public Adapter_groupBook(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return new fragment_groupBook_krippe();
            case 1:
                return new fragment_groupBook_hafen();
            case 2:
                return new fragment_groupBook_kindergarten();
            default:
                return null;
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

奇怪的是,每当我从子片段中删除RelativeLayout并只使用ConstraintLayout时,内容就会变空。我尝试的每一个更改或替代都以一个空的子片段结束。
PS:对我来说,底部的navBar也发生了同样的情况,但我认为它可能与tabLayout/fragment问题有相同的原因-所以也许我可以在这里得到解决方案/帮助后自己修复。
提前感谢你的帮助。

62o28rlo

62o28rlo1#

将视图页包含在相对布局中并设置android:layout_centerInParent=“true”
使用此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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorAnimationMode="elastic" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabLayout">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager_groupBook"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

相关问题