协调布局,工具栏下方的活动界面。

11

我有一个使用CoordinatorLayout和滑动选项卡以及ViewPager的应用程序。我尝试将一个活动(列表)放置在工具栏下方。我尝试了许多不同的方法,但总是列表会重叠到工具栏上面。

布局

<?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" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            android:padding="2dp"
            app:titleMarginStart="20dp"
            android:theme="@style/AppTheme.AppBarOverlay"/>

        <com.passwordstore.utility.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary" />

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@android:color/white"
            android:foreground="@drawable/shadow"/>

       <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>

    </LinearLayout>

    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/frameLayout">

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

    </FrameLayout>

    <android.support.design.widget.FloatingActionButton android:id="@+id/fabNew"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_add_white_24dp"
        app:layout_anchor="@+id/pager"
        app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>
3个回答

26
只需在需要在工具栏下方的布局中添加app:layout_behavior="@string/appbar_scrolling_view_behavior"即可。

3
请尝试这个:
<?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" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            android:padding="2dp"
            app:titleMarginStart="20dp"
            android:theme="@style/AppTheme.AppBarOverlay"/>

        <com.passwordstore.utility.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary" />

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@android:color/white"
            android:foreground="@drawable/shadow"/>

       <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>

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

    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_below= "@id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/frameLayout">

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

    </FrameLayout>

    <android.support.design.widget.FloatingActionButton android:id="@+id/fabNew"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_add_white_24dp"
        app:layout_anchor="@+id/pager"
        app:layout_anchorGravity="bottom|right|end"/>

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

我删除了 linearLayout 并用 appBarLayout 替换它,同时在你的 Framelayout 中添加了 android:layout_below

祝好运


我无法使用AppBarLayout,因为我的列表不随ViewPager滚动,所以不得不更改它。 - Carl
好的,请保留你的LinearLayout,只需要在FrameLayout中添加android:layout_below="@id/yourLinearLayoutID",看看是否解决了这个问题。 - Netero
1
在CoordinatorLayout中不能使用layout_below。 - Carl
它不起作用了。底部视图没有显示。请为此提供建议。 - Anand Savjani

0
您可以用一个纵向排列的LinearLayout(android:orientation=”vertical”)替换CoordinatorLayout。
<RelativeLayout>
    <LinearLayout> (replaces the coordinator layout>
        <AppBarLayout/>
        <FrameLayout/>
    </LinearLayout>
    <FloatingActionButton/>
</RelativeLayout>

然后我遇到了浮动操作按钮的问题。 - Carl
将此新LinearLayout放置在RelativeLayout中。并且将FAB放置在与LinearLayout相同的级别上(RelativeLayout的直接子视图)。 - Fundhor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接