首页 技术 正文
技术 2022年11月14日
0 收藏 844 点赞 4,930 浏览 5054 个字

原文地址 http://www.cnblogs.com/Dentist/p/4370176.html

Android4.0出现的Actionbar提供了同意方便的导航管理。很大程度的统一了Android应用的风格,但他定制性差,使用不灵活的缺点限制了Android应用的发展。

Android5.0谷歌推出了Material Design。用ToolBar代替了Actionbar。继承了Actionbar的用法。却像布局一样任意修改。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" > <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/></LinearLayout>

然后代码里找到并设置为SupportActionBar就成为了一个真正的Actionbar。不过theme里关于ActionBar的属性对他不管用。因为他是假的,是Google的特技。

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

但 onCreateOptionsMenu  和  onOptionsItemSelected都有用。

设置导航图标 toolbar.setNavigationIcon();

但设置标题则需要用 getSupportActionBar().setTitle();

使用ToolBar时一定要把Activity的Theme设为NoActionBar。或者Theme.AppCompat并重写这俩属性

        <item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>

因为Theme.AppCompat.NoActionBar仅仅就是在Theme.AppCompat的基础上多加了上面两行。看源码便知。

然后你一定会发现:

当你使用Light主题时。APP所有字的颜色都是黑色,包括Toolbar的字体颜色也是黑色。

当你使用Dark(普通)主题时。APP所有字的颜色都是白色,包括Toolbar的字体颜色也是白色。就像这样:

Toolbar使用Toolbar使用

而google的Material Design展示的优雅界面

Toolbar使用

这TM是在逗我。。。

其实ToolBar支持单独设置一个theme

    <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/Theme.AppCompat">
</android.support.v7.widget.Toolbar>

然后给Activity设置为Light主题。就完美解决了。

但每个页面都写这样一堆XML代码洁癖患者实在受不了。

可以使用include标签优雅的解决。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/Theme.AppCompat"
android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"> <include layout="@layout/toolbar" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/></LinearLayout>

注意当你换了Toolbar的主题就不能再用 android:background=”?attr/colorPrimary” 这种基于主题的资源,应该直接重新定义。

然后介绍下ToolBar的特技。

1.动画显示/隐藏ToolBar

    public void showToolbar(boolean show){
if (show == toolbarShow || toolbar == null)return;
toolbarShow = show;
if (show) {
toolbar.animate()
.translationY(0)
.alpha(1)
.setDuration(HEADER_HIDE_ANIM_DURATION)
.setInterpolator(new DecelerateInterpolator());
} else {
toolbar.animate()
.translationY(-toolbar.getBottom())
.alpha(0)
.setDuration(HEADER_HIDE_ANIM_DURATION)
.setInterpolator(new DecelerateInterpolator());
}
}

2.设置ToolBar的背景透明度

//设置好ToolBar过后
mActionbarDrawable = new ColorDrawable(getResources().getColor(R.color.theme_primary));
getSupportActionBar().setBackgroundDrawable(mActionbarDrawable);

  

    protected void setToolbarAlpha(float alpha){
mActionbarDrawable.setAlpha((int) (alpna*255));
}

这两个特技一般都配合RecyclerView SccollView等滑动视图(过时的ListView不考虑了)。

RecyclerView :

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {  @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Utils.Log("percent:" + dy);
if (dy > 1){
onHide();
}else if(dy < -1){
onShow();
}
} public abstract void onHide();
public abstract void onShow();}
mRecyclerView.setOnScrollListener(new HidingScrollListener() {
@Override
public void onHide() {
showToolbar(false);
} @Override
public void onShow() {
showToolbar(true);
}
});

SccollView:

public class ObservableScrollView extends ScrollView {
public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); }
private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) {
super(context);
} public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
} public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
} public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
} @Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
} }
        mScrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y-oldy>3){
showToolbar(false);
}else if(y-oldy<-3){
showToolbar(true);
}
}
});
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741