【问题标题】:ViewPager not showing up with CoordinatorLayoutViewPager 未与 CoordinatorLayout 一起显示
【发布时间】:2015-09-18 21:15:55
【问题描述】:

我的应用程序的操作栏由一个工具栏和一个带有 ViewPager 的选项卡视图组成,用于显示页面的内容。我正在尝试使用 CoordinatorLayout 而不是 LinearLayout 作为我的根视图,但是当我将 LinearLayout 替换为 android.support.design.widget.CoordinatorLayout 时,我的 ViewPager 的内容不再可见。这是以 LinearLayout 为根的 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">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <include layout="@layout/actionbar" />
        </android.support.v7.widget.Toolbar>

        <com.whatsgoodly.views.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity" />
</LinearLayout>

这是应用程序的屏幕截图,其中android.support.design.widget.CoordinatorLayout 作为根视图。如您所见, ViewPager 中的内容被隐藏/丢失。 (我把选项卡图标和操作栏文本都用photoshop去掉了)

我做错了什么?

【问题讨论】:

    标签: android android-layout android-fragments android-viewpager android-coordinatorlayout


    【解决方案1】:

    也许我的对你有帮助。只需自定义内容:

    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activity">
    
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/primary_dark"
                android:theme="@style/toolbarstyle"/>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable" />
            <include layout="@layout/toolbar_shadow" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:background="@color/white" />
    
    
    
        </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout> 
    

    【讨论】:

    • 太好了,我的内容出现了!但是,我使用的是CoordinatorLayout,这样工具栏会在滚动时折叠,但这仍然没有发生。我将标签 app:layout_scrollFlags="scroll|enterAlways" 添加到我的工具栏 XML 视图中,但它仍然没有崩溃。有什么想法吗?
    • 嗯,我以前从未使用过折叠工具栏,但我看到了该 [链接] 的教程(下面有一个标签布局:youtube.com/watch?v=aqvwA58b9l8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多