makisang / android

0 stars 0 forks source link

CoordinatorLayout和Toolbar #3

Open makisang opened 8 years ago

makisang commented 8 years ago

CoordinatorLayout 是 google 推出的一个控件,利用它可以实现 MD 风格的布局风格

makisang commented 8 years ago

一般 CoordinatorLayout 里面有一个 AppBarLayout, 用来放置 ToolBar, 然后底下是界面内容

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="?android:attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="@string/toolbar_import"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

要让 RecyclerView 处于 ToolBar 下方,必须加上这句话 app:layout_behavior="@string/appbar_scrolling_view_behavior"

makisang commented 7 years ago

切换fragment时让隐藏的Toolbar显示出来

// Show toolbar.
CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.tabParent);
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.app_bar_layout);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
// int[] consumed = new int[2];
// behavior.onNestedPreScroll(coordinator, appbar, null, 0, -1000, consumed);
behavior.onNestedFling(coordinator, appbar, null, 0, -1000, true);
makisang commented 7 years ago

改变Toolbar中的icon

stackoverflow