如何创建重叠CoordinatorLayout内容的AppBarLayout

11

在一些活动中,当我使用CoordinatorLayoutAppBarLayout时,我需要内容位于AppBarLayout下方。也就是说,工具栏使用了一些透明颜色并覆盖了内容。默认情况下,CoordinatorLayout+AppBarLayout会安排工具栏和滚动内容相邻,而不重叠。

Android开发者指南在这里有相关文档,看起来像这样(但这些标志似乎不能与Toolbar和appcompat一起使用-我尝试过):

覆盖ActionBar

所以我需要像上面的图片一样的东西,但同时还要使用由CoordinatorLayout+AppBarLayout提供的所有滚动功能。而且不需要使用CollapsingToolbarLayout - 只需使用这个简单的工具栏。

如何实现这一点?这是我的活动布局。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/top_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <include layout="@layout/main_toolbar"/>
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
        <!-- to be filled by content fragment -->
    </FrameLayout>
    <android.support.design.widget.FloatingActionButton
        style="@style/FabStyle"
        android:id="@+id/fab_button"
        android:src="@drawable/bt_filters"
        />
</android.support.design.widget.CoordinatorLayout>

http://inthecheesefactory.com/blog/android-design-support-library-codelab/en - Jude Fernandes
@JudeFernandes 这里描述了一个使用CollapsingToolbarLayout的设置,该设置由某个图像支持。我描述了另一种情况 - 我需要活动内容在透明工具栏下方。而且我不需要上面提到的CollapsingToolbarLayout - dimsuz
你的AppBar在滚动内容时是否仍需要隐藏/显示,还是可以保持静态?只有Activity背景需要位于工具栏下方吗,还是Activity内容也需要? - Jeroen Mols
4个回答

6

我尝试了这个解决方案,它有效。

透明度: 向AppBarLayout添加了背景,并将可滚动视图放置在AppBarLayout之前的布局中。

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000" >

内容定位: 通过创建一个新的AppbBarTransparentScrollingViewBehavior,扩展了AppBarLayout.ScrollingViewBehavior的功能,并重写了onDependentViewChanged()方法和修改了updateOffset()方法为offset = 0

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
        View dependency) {
    updateOffset(parent, child, dependency);
    return false;
}

private boolean updateOffset(CoordinatorLayout parent, View child,
        View dependency) {
    final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency
            .getLayoutParams()).getBehavior();
    if (behavior instanceof Behavior) {
        // Offset the child so that it is below the app-bar (with any
        // overlap)
        final int offset = 0;   // CHANGED TO 0
        setTopAndBottomOffset(offset);
        return true;
    }
    return false;
}

新内容的行为:在滚动视图上设置行为。

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout_behavior="AppbBarTransparentScrollingViewBehavior" />

结果: 在一个嵌套滚动视图中放置ImageView作为滚动视图

enter image description here


这是整个stackoverflow和更多的唯一可行答案,适用于糟糕的“com.android.support:appcompat-v7:23.1.1”。太棒了,非常感谢! - kellogs
2
很好的答案。在定义自定义行为时,不要忘记添加带有上下文和attributeSet参数的构造函数(只需在其中调用super()),否则在填充布局时会遇到一些问题。 - fstephany

3

如果您移除了这行代码

app:layout_behavior="@string/appbar_scrolling_view_behavior"

来自FrameLayout,内容将被Toolbar覆盖。希望可以帮到您。

编辑:哦,您提到需要滚动功能,所以这并不是一个解决方案。


1
我会尝试将主背景图片应用到windowBackground,并将工具栏/操作栏的背景设置为透明。下面是我在清单文件中指定的样式。可以根据需要更改窗口背景。
<style name="AppThemeSliderToolbar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/t_img</item>
        <item name="colorPrimary">#ff5b45</item>
        <item name="colorPrimaryDark">#FF5722</item>
    </style>

使用半透明背景的AppBar布局
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            >
            <!--Change Opacity background as per required ..android:background="#50000000"-->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways" />
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/frgmentcontainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appbar"></FrameLayout>


    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@android:drawable/ic_dialog_email"
        app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>

更新

根据我们在评论 CollapsingToolbarLayout 与片段的讨论。

<?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=".ScrollingActivity">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height"  android:background="#00FFFFFF"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true" android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

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

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

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

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

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

content_scrolling.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_scrolling" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".ScrollingActivity">
    <FrameLayout android:id="@+id/framcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

</android.support.v4.widget.NestedScrollView>

样式在清单文件中赋予了活动。

  <style name="AppThemeSliderToolbar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/t_img</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="colorPrimary">@android:color/transparent</item>
    </style>

滚动活动(ScrollingActivity)
public class ScrollingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportFragmentManager().beginTransaction().
                replace(R.id.framcontainer, new HomeFragment(), "Home").commit();
    }

}

主页2

public class Home2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_2, container, false);
    }
}

HomeFragment
public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.homefragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.framcontainer, new Home2(), "Home2").addToBackStack("Home2").commit();
            }
        });
    }
}

屏幕截图网址。避免嵌入图片,我已提供了网址。

更新前答案 http://i.stack.imgur.com/5cVOw.jpg
从更新后的答案中的HomeFragment > http://i.stack.imgur.com/UF8LW.jpg
从更新后的答案中的Home2 http://i.stack.imgur.com/cD480.jpg


我们能否在Fragment中不创建新的工具栏布局来完成同样的事情? - user2095470
@user2095470,我在活动中通常使用片段和工具栏。你能否请再解释一下,这样我就可以尝试一下呢? - user1140237
感谢您的努力。您认为在此片段中仅添加可折叠图像是否可能(假设我在应用程序中有多个片段)。所有其他片段将具有普通的活动工具栏,没有可折叠视图。 - user2095470
@user2095470 是的,这是可能的...实际上,在问题中CollapsingToolbarLayout不存在,所以我没有在我的xml中使用它。这是可能的。 - user1140237
@user2095470 如果有时间会尝试 :) - user1140237
显示剩余4条评论

0

对于任何正在使用NavigationDrawer的布局的人来说,解决方案之一是在渲染该片段时动态更改ToolBar/AppBar。例如,假设您从Home.java开始并想要转到Chats.Java,这是一个片段,您想要更改该工具栏,则可以执行以下操作:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

            case R.id.nav_chat:

                toolbar.inflateMenu(R.menu.My_Other_Menu);
                toolbar.setTitle("My New Title");
               
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);

                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PaymentFragment()).commit();
                break;
    }

}

请注意,我们已更改标题并更改了标题、菜单,以及将默认的NavigationIcon更改为返回按钮。但是,当单击返回按钮时,我们还必须将其更改为返回上一页。
此解决方案将Chats.java的内容推到ToolBar下方,而不是被其覆盖。

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