为什么在Android中切换片段后会忽略工具栏?

时间:2019-06-22 12:20:18

标签: android android-layout android-relativelayout android-framelayout bottomnavigationview

我正在使用// This part is the same.. httpget.setConfig(requestConfig); ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<CloseableHttpResponse> callable = () -> { try (CloseableHttpResponse response = httpclient.execute(httpget)) { return response; } }; Future<CloseableHttpResponse> future = executorService.submit(callable); try { future.get(4, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { httpget.abort(); executorService.shutdownNow(); } 构建的应用程序,其中第一个标签页已正确加载:

标签1:

img1

但是,当我切换到下一个选项卡时,Framelayout似乎在工具栏下方,如下所示:

标签2:

img 2

再次

选项卡1:

img 3

这是我的布局:

BottomNavigationView

也有类似的情况:

Part of Fragment items hides under Action bar

但是对于我而言,解决方案不起作用。知道我应该改变什么吗?还是为什么工具栏失去优先级?我已经测试过自己在版式中创建一个,并且也无法正常工作。

PS

我正在加载一些 WebViews ,但我怀疑它是否会影响

3 个答案:

答案 0 :(得分:0)

我在项目和工作中使用它:

<android.support.constraint.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=".app.main.MainActivity">

<FrameLayout
    android:id="@+id/frame_fragment_containers"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
    android:layout_above="@id/bottom_navigation"/>




    <android.support.design.widget.BottomNavigationView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:itemBackground="@color/colorWhite"
        app:itemIconTint="@color/selector_bottom_navigation"
        app:itemTextColor="@color/selector_bottom_navigation"
        android:layout_alignParentBottom="true"
        >
    </android.support.design.widget.BottomNavigationView>

,您可以以编程方式使用它:

 bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
    bottomNavigation.inflateMenu(R.menu.bottom_menu);
    // change to whichever id should be default
    if (savedInstanceState == null) {
        bottomNavigation.setSelectedItemId(R.id.home);
    }
    fragmentManager = getSupportFragmentManager();
    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch (id) {

                case R.id.profile:
                    fragment = new FragmentD();
                    break;
                case R.id.support:
                    fragment = new FragmentC();
                    break;
                case R.id.order:
                    fragment = new FragmentB();
                    break;
                case R.id.home:
                    fragment = new FragmentA();
                    break;

            }
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frame_fragment_containers, fragment).commit();
            return true;
        }
    });

并在菜单文件夹中为项目BottomNavigation创建新的xml (bottomNavigation.inflateMenu(R.menu.bottom_menu))

<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">



<item
    android:id="@+id/profile"
    android:title="Profile"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_profile">
</item>
<item
    android:id="@+id/support"
    android:title="Support"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_support">
</item>
<item
    android:id="@+id/order"
    android:title="Order"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_orders">
</item>
<item
    android:id="@+id/home"
    android:title="Home"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_home">
</item>

答案 1 :(得分:0)

您的代码对我来说很好。尽管您可以为框架布局设置页边距顶部

conf

答案 2 :(得分:0)

我发现了如何使用 CoordinatorLayout 对其进行修复:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"            
            android:layout_above="@+id/bottom_navigation">
            <FrameLayout
                android:id="@+id/content_frame"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.design.widget.CoordinatorLayout>
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_nav_menu"
            app:labelVisibilityMode="unlabeled"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@color/bottom_nav_color"
            app:itemTextColor="@color/bottom_nav_color" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

我从这里有了一个主意:

https://forums.xamarin.com/discussion/146285/how-to-do-appbarlayout-with-collapsing-toolbarlayout-that-will-collapse-when-viewpager-scroll