为什么应用程序栏将视图分页器向下推

时间:2019-05-19 02:29:36

标签: android android-layout

我尝试将应用栏的高度设置为其子项的总和+状态栏的高度,但最终还是通过将视图分页器向下推来实现。这是问题的图片:https://imgur.com/a/JQYpxgD

这是布局:

<android.support.design.widget.CoordinatorLayout
        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:fitsSystemWindows="true"
        tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:background="@drawable/side_nav_bar"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

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

这是视图寻呼机的布局:

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/contentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.constraint.ConstraintLayout>

这是用于设置应用栏高度的代码 :

    private fun setAppBarHeight() {
        val appBarLayout: AppBarLayout = findViewById(R.id.appbar)
        appBarLayout.layoutParams.height = getStatusBarHeight() + dpToPx(48 
    + 56)
    }

    private fun getStatusBarHeight(): Int {
        var result = 0
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    private fun dpToPx(dp: Int): Int {
        val density = resources
            .displayMetrics
            .density
        return Math.round(dp.toFloat() * density)
    }

1 个答案:

答案 0 :(得分:0)

从应用栏布局内部移动标签布局并放置在下面

<app bar
  <tooolbar
  </toolbar>
</app bar>
<tab layout/>
<view pager/> blah
相关问题