从具有折叠工具栏的选项卡切换时,Bottomnavigationview隐藏

时间:2017-07-23 17:26:21

标签: android

我有一个MainActivity XML,其中包含一个带有5个标签的bottomNavigationView(条形)。每个选项卡调用不同的片段。从具有折叠工具栏的片段(在图片中标记为A)切换到具有普通片段(在图片中标记为B)的片段会导致bottomNavigationView(条)部分隐藏在屏幕外。

有没有办法阻止这种情况?

包含折叠工具栏的片段的屏幕。

Link: screen with fragment containing collapsing toolbar

另一个带有普通片段的标签的屏幕。

Link: screen of another tab with a plain fragment

包含用于折叠工具栏的代码的片段类

 public class Profile extends Fragment {

    public Profile() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //setHasOptionsMenu(true);

        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

        Toolbar toolbar = rootView.findViewById(R.id.toolbar);

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        CollapsingToolbarLayout collapsingToolbar = rootView.findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("test");

        return rootView;
    }

包含折叠工具栏代码的对应xml

<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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/ani_dog_one"
                android:contentDescription="whut"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

普通片段类

public class Review extends Fragment {

 public Review() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_review, container, false);
        //setHasOptionsMenu(true);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.search_bar_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

普通片段类的XML只包含一个简单的FrameLayout

MainActivity

public class MainActivity extends AppCompatActivity {

private Intent intent;

private android.support.v4.app.FragmentManager manager;
private android.support.v4.app.FragmentTransaction transaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager = getSupportFragmentManager();          
    transaction = manager.beginTransaction();      

    Discover discoverFragment = new Discover();
    transaction.replace(R.id.container, discoverFragment, discoverFragment.getTag()).commit();

    setupBottomNavigationView();

}

private void setupBottomNavigationView() {

BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(this, bottomNavigationViewEx);

}

public void goToOptions(MenuItem menu) {

 intent = new Intent(this, Options.class);
overridePendingTransition(R.anim.left_in, R.anim.right_out);
startActivity(intent);
}

}

MainActivity的XML

    <LinearLayout 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"
        android:orientation="vertical"
        tools:context="com.example.android.project_qwer.MainActivity">

    <!-- main fragments goes here -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <!-- bottom navigation view -->
    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation_menu" >

    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

</LinearLayout>

5 个答案:

答案 0 :(得分:1)

尝试删除

  

机器人:fitsSystemWindows =&#34;真&#34;

在您的CollapsingToolbarLayout上

答案 1 :(得分:0)

尝试

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

切换到带有折叠工具栏和

的标签页时
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

切换到另一个

答案 2 :(得分:0)

尝试放

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

</android.support.design.widget.CoordinatorLayout>

作为从BottomNavigationView加载的所有其他片段的根。

答案 3 :(得分:0)

如果您在配置文件片段中使用自定义工具栏,请将此代码放入onCreateView方法中:

((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

我遇到了同样的问题,并且解决了。

答案 4 :(得分:0)

删除android:fitsSystemWindows="true" 来自根CoordinatorLayout