11

在某些活动上使用CoordinatorLayoutwithAppBarLayout时,我需要将内容放在AppBarLayout 下,即工具栏使用一些透明颜色并覆盖内容。默认情况下CoordinatorLayout+AppBarLayout安排东西,使工具栏和滚动内容彼此相邻,没有任何重叠。

Android 开发人员指南在此处有相关文档,看起来像这样(但这些标志似乎不适用于 Toolbar 和 appcompat - 我试过了):

叠加操作栏

所以我需要一些看起来像上图的东西,但是所有的滚动都是由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>
4

4 回答 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 覆盖onDependentViewChanged()并修改updateOffset()为扩展 AppBarLayout.ScrollingViewBehavioroffset = 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" />

结果:在 NestedScrollView 中使用 ImageView 作为滚动视图

在此处输入图像描述

于 2015-12-16T14:06:10.580 回答
3

如果您删除该行

app:layout_behavior="@string/appbar_scrolling_view_behavior"

从 开始FrameLayout,内容会被 重叠Toolbar。希望有帮助。

编辑:哦,你提到你需要滚动的好东西,所以这不是一个真正的解决方案。

于 2015-12-14T17:13:22.847 回答
1

在这里,我尝试将主背景图像windowBackground和 ToolBar/ActionBar 背景作为透明。在我在清单中指定的样式下方。可以根据需要更改窗口背景。 Menifest 中的风格

<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>

滚动活动

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);
    }
}

首页片段

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();
            }
        });
    }
}

截图网址。避免 Ambeding 图片我给了 url

更新前的 Ans http://i.stack.imgur.com/5cVOw.jpg
HomeFragment 来自更新的 Ans > http://i.stack.imgur.com/UF8LW.jpg
Home2 来自更新的 Ans http://i.stack。 imgur.com/cD480.jpg

于 2015-12-19T12:07:20.973 回答
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 下方,而不是被它覆盖。

于 2022-01-27T11:45:52.633 回答