- 关于
Toolbar
的使用,现在已经可以在网上搜索到很多有用的资源了。但是,真TM遗憾,没有一篇真的令我满意的。不清楚是这些人的表达能力不够,还是我的理解能力的欠缺。 - OK,既然逼已经装过了,那么就来分享一下我那蹩脚的实现吧。
- 我就一句废话也不说了,先上代码吧。因为代码真的很少。我是在
Activity
中去实现的。 - 代码:1/2 :
public class ToolbarActivity extends AppCompatActivity {
public Context get() {
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_toolbar);
}
}
- 代码 2/2:
<?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"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#30469b"
app:layout_scrollFlags="scroll|enterAlways|snap" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#3f00"
android:orientation="vertical">
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<!-- content view 好像这里只能是 NestedScrollView 否则无法带动上面一起滚动-->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/big_txt"
android:textSize="18sp" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
- 好了,代码结束了。
Toolbar
的收缩功能就实现了。 - 这里稍微说明一下:
- xml中 一定要加
app:layout_scrollFlags="scroll|enterAlways|snap"
这个。是为了让下面的滑动view去控制收缩的。
- xml中 一定要加
<android.support.v7.widget.Toolbar
......
app:layout_scrollFlags="scroll|enterAlways|snap" />
(markdown的排版其实挺操蛋的!)
- 下面的滚动布局,貌似只能是
NestedScrollView
.虽然我看到别人的代码中貌似可以是任何的滚动View,但是我的实践结果告诉我,我只知道NestedScrollView
可以,其他的还不知道是怎么实现的。 至于xml中要不要加属性
app:layout_behavior="@string/appbar_scrolling_view_behavior"
,从实践结果来看,可有可无。关于5.x以上的这些新控件,感觉这几个非常的不透明,这些属性都不能在`android studio’中直接找出来,即使,你定义命名空间。而且,使用起来,也让人非常的困惑。
- 下面的滚动布局,貌似只能是