Viewpager和导航栏位于其顶部

时间:2016-11-08 14:07:30

标签: android android-fragments android-viewpager android-navigation-bar

似乎这个问题之前已被多次询问过。 至少我在互联网上发现了很多东西,特别是SO。 但是没有一个答案有帮助,也不知道为什么。

我在Android Studio 2.2.2中创建了一个简单的空白选项卡式活动项目 除了一个添加之外,Haven没有触及任何内容 - 添加了一个imageview。 这是一个生成的活动布局:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.company.me.navbartest.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

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

        </android.support.v7.widget.Toolbar>

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

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

这里没有改变任何东西。 这是一个片段的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.company.me.navbartest.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_alignParentBottom="true"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

这里唯一的补充是片段底部的ImageView。 根本没有触摸代码。不知道是否需要它,但万一它在这里:http://pastebin.com/FuUiGpyH

结果如下: Navbar troubles

这是在KitKat上拍摄的,但在Marshmallow上也是如此。 看到绿色的小耳朵?来自ImageView的那个我已经添加了。它是android徽标的图标。

我尝试过不同的&#34; fitsSystemWindows&#34;旗。没有成功。我想我不太明白它是如何工作的,应该被使用。

还尝试更改片段的根对象 - 结果相同。

所以主要的问题是 - 如何解决这个问题?如何确保这个ImageView显示在它应该的位置?

gradle规格是:

minSdkVersion 19
targetSdkVersion 25
compileSdkVersion 25
buildToolsVersion "25.0.0"
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'

1 个答案:

答案 0 :(得分:1)

看起来我找到了解决方案。

&#34;问题&#34;在工具栏的滚动标记中。我想该模板应该与列表和特定滚动(工具栏折叠的那个)一起使用,因此标志是

scroll | enterAlways

但我的寻呼机没有任何列表,并且不应该是可滚动的,所以我只是删除了它们,现在一切正常了!

另一个&#34;解决方法&#34;是在LinearLayout中包围AppBarLayout。也有帮助,但不知道副作用。 Haven没有对它进行过多次测试。

相关问题