修复Botttom导航栏的片段包含Coordinator布局时继续扩展的问题

时间:2019-05-17 02:03:16

标签: android android-layout

我在底部的导航栏活动中有一个片段。 在3个菜单中,有1个存在一个片段,该片段使用Coordinator布局作为其父级,并带有App栏布局和折叠工具栏。相对于父级布局父级,另一个菜单可以很好地工作,但是具有协调器布局的片段不能很好地工作,并且会使底部导航栏自行扩展其高度。在这里,我提供了我的代码和一些屏幕截图。谢谢

这是我的片段布局的代码

   <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:background="#FFF"
                app:layout_collapseMode="parallax">
                <TextView
                    android:id="@+id/nama_user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="20dp"
                    android:layout_marginStart="10dp"
                    android:text="@string/nama_sample"
                    android:textSize="20dp"
                    android:textStyle="bold"/>
                <TextView
                    android:id="@+id/lokasi_user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lokasi_sample"
                    android:layout_alignStart="@id/nama_user"
                    android:layout_below="@id/nama_user"
                    android:layout_alignEnd="@id/foodie_user"
                    android:layout_marginTop="5dp"
                    android:drawableStart="@drawable/ic_location_on_black_24dp"
                    android:gravity="center"
                    android:textStyle=""/>
                <TextView
                    android:id="@+id/foodie_user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/foodie_sample"
                    android:layout_alignStart="@id/nama_user"
                    android:layout_below="@id/lokasi_user"
                    android:layout_marginTop="5dp"
                    android:drawableStart="@drawable/ic_foodie"
                    android:drawablePadding="5dp"
                    android:layout_marginBottom="20dp"
                    android:gravity="center"
                    android:textColor="@color/color_yellow"/>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_alignTop="@id/lokasi_user"
                    android:layout_alignBottom="@id/foodie_user"
                    android:layout_alignEnd="@id/nama_user"
                    android:gravity="center_horizontal">
                    <TextView
                        android:id="@+id/review_count_user"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="46"
                        android:textSize="25dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Reviews"/>
                </LinearLayout>
                <ImageView
                    android:id="@+id/image_user"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="10dp"
                    android:src="@drawable/ic_person_black_24dp"
                    android:tint="@color/color_grey"
                    />
            </RelativeLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/color_red" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
    </android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>


这是我的活动代码,带有底部导航栏

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">
    <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/nav_view"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#6DCCD1D8"
        android:layout_above="@id/nav_view"/>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

这是它的活动java

public class HomeActivity extends AppCompatActivity implements
        DiscoverFragment.OnFragmentInteractionListener,
        SearchFragment.OnFragmentInteractionListener,
        ProfileFragment.OnFragmentInteractionListener{
    FrameLayout fragment;
    final Fragment fragment1 = new DiscoverFragment();
    final Fragment fragment2 = new SearchFragment();
    final Fragment fragment3 = new ProfileFragment();
    final FragmentManager fm = getSupportFragmentManager();
    Fragment active = fragment1;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.discover_home:
                    fm.beginTransaction().hide(active).show(fragment1).commit();
                    active = fragment1;
                    return true;
                case R.id.search_home:
                    fm.beginTransaction().hide(active).show(fragment2).commit();
                    active = fragment2;
                    return true;
                case R.id.profile_home:
                    fm.beginTransaction().hide(active).show(fragment3).commit();
                    active = fragment3;
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        fragment = findViewById(R.id.fragment_holder);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        fm.beginTransaction().add(R.id.fragment_holder, fragment3, "3").hide(fragment3).commit();
        fm.beginTransaction().add(R.id.fragment_holder, fragment2, "2").hide(fragment2).commit();
        fm.beginTransaction().add(R.id.fragment_holder,fragment1, "1").commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

This is how my error looks like

Here is a screenshot of my error when I select profile tab

选择个人资料栏时便弹出

谢谢:)

1 个答案:

答案 0 :(得分:1)

请尝试从XML中删除此行

`android:fitsSystemWindows="true" 

因为它设置了View的填充,以确保内容不会覆盖系统窗口。