2015-10-19 122 views

回答

1

展开/收起Toolbar使用CoordinatorLayoutAppBarLayoutsetExpanded(boolean expanded)(支持库V23 +)一FloatingActionButton点击

<android.support.design.widget.CoordinatorLayout 
    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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <!--This view depends heavily on being used as a direct child within a CoordinatorLayout--> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbarLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#b71c1c" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      android:minHeight="96dp" 
      app:layout_collapseMode="pin" 
      app:layout_scrollFlags="scroll|enterAlways"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Expandable/Collapsible Toolbar" /> 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 

    <RelativeLayout 
     android:id="@+id/v2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="24dp" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Content" /> 
    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     android:src="@drawable/ic_add_white_24px" 
     app:layout_anchor="@id/v2" 
     app:layout_anchorGravity="top|right|end" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

appBarLayout.setExpanded(真/假)

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final AppBarLayout appbarLayout = (AppBarLayout)findViewById(R.id.appbarLayout); 
    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab); 

    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (appbarLayout.getTop() < 0) 
       appbarLayout.setExpanded(true); 
      else 
       appbarLayout.setExpanded(false); 
     } 
    }); 
} 

Image: Expand/Collapse Toolbar with FAB click