以编程方式设置工具栏layout_collapseMode

时间:2015-09-06 13:44:05

标签: android android-layout material-design

有什么方法可以通过编程方式更改我的android.support.v7.widget.Toolbar layout_collapseMode属性吗?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="?attr/actionBarSize"
    app:layout_collapseMode="pin"
    app:theme="@style/ToolBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>

2 个答案:

答案 0 :(得分:9)

试试这个

ViewGroup.LayoutParams params = toolbar.getLayoutParams();
CollapsingToolbarLayout.LayoutParams newParams;
if (params instanceof CollapsingToolbarLayout.LayoutParams) {
  newParams = (CollapsingToolbarLayout.LayoutParams)params;
} else {
  newParams = new CollapsingToolbarLayout.LayoutParams(params);
}
newParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_OFF);
toolbar.setLayoutParams(newParams);
toolbar.requestLayout();

https://developer.android.com/reference/android/support/design/widget/CollapsingToolbarLayout.LayoutParams.html

答案 1 :(得分:0)

  <android.support.design.widget.CoordinatorLayout  
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/tabanim_maincontent"
     android:layout_width="match_parent"
     android:layout_height="match_parent">     

   <com.gplanet.qpeople.widget.ControllableAppBarLayout
       android:id="@+id/tabanim_appbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:layout_scrollFlags="exitUntilCollapsed"
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tabanim_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


    <android.support.design.widget.TabLayout
        android:id="@+id/tabanim_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_scrollFlags="enterAlways" />
</com.gplanet.qpeople.widget.ControllableAppBarLayout

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

ControllableAppBarLayout https://gist.github.com/blipinsk/3f8fb37209de6d3eea99

你可以打电话

appBarLayout.expandToolbar(true); // true to animate when expand,
appBarLayout.collapseToolbar(true);