讲一讲 ,为什么写这篇文章,也许就是想写的什么吧,写代码的时候经常碰到问题,看了很多大神的博客,帮了很多忙。虽然本人编码水平也很一般,但是如果能帮助到一些人,那就很开心了。
本文参考了 https://blog.csdn.net/jxxfzgy/article/details/46476903 钟光亮的文章,谢谢。
现在开始正文:
ToolBar就是谷歌放出来取代ActionBar的,他可以使状态栏,更加容易定制。
ToolBarActivity 继承了实现左侧返回的SwipeBackActivity
在gralde中添加 compile 'org.dync.kathline:swipebackactivity:1.0.1'
package com.toman.toolbar;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.swipebackactivity.app.SwipeBackActivity;
import com.toman.R;
/**
* @Description:
* @Author:
* @Since:2016/12/16 10:54
*/
public abstract class ToolBarActivity extends SwipeBackActivity {
private ToolBarHelper mToolBarHelper;
public Toolbar toolbar;
private TextView tvTitle;
public Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBarColor(getResources().getColor(R.color.colorPrimary));
}
@Override
public void setContentView(int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
// mToolBarHelper.setToolbarTitle("a");
toolbar = mToolBarHelper.getToolBar();
setContentView(mToolBarHelper.getContentView());
/*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar);
/*自定义的一些操作*/
onCreateCustomToolBar(toolbar);
// mToolBarHelper.setToolbarTitle("a");
}
public void onCreateCustomToolBar(Toolbar toolbar) {
tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
toolbar.setContentInsetsRelative(0, 0);
toolbar.inflateMenu(R.menu.toolbar_act_details);
}
@Override
protected void onResume() {
/**
* 设置为横屏
*/
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onPause() {
super.onPause();
}
public void initToolBarNew(String text, int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
mToolBarHelper.setTvTitle(text);
super.setTitle("");
setContentView(mToolBarHelper.getContentView());
toolbar = mToolBarHelper.getToolBar();
/*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar);
}
public void initToolBarNew(String text, @ColorInt int colorId, int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
mToolBarHelper.setTvTitle(text);
mToolBarHelper.setToolBarColor(colorId);
setStatusBarColor(colorId);
super.setTitle("");
setContentView(mToolBarHelper.getContentView());
toolbar = mToolBarHelper.getToolBar();
/*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar);
}
public void setTvTitle(String text){
mToolBarHelper.setTvTitle(text);
}
}
ToolBarHelper toolbar工具类
package com.toman.toolbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.toman.R;
/**
* @Description:toolbar帮助工具类
* @Author:
* @Since:2016/12/16 10:55
*/
public class ToolBarHelper {
/*上下文,创建view的时候需要用到*/
private Context mContext;
/*base view*/
private FrameLayout mContentView;
/*用户定义的view*/
private View mUserView;
/*toolbar*/
private Toolbar mToolBar;
/*视图构造器*/
private LayoutInflater mInflater;
/*
* 两个属性
* 1、toolbar是否悬浮在窗口之上
* 2、toolbar的高度获取
* */
private TextView tv_toolbar_title;
private static int[] ATTRS = {
R.attr.windowActionBarOverlay,
R.attr.actionBarSize
};
int mLayoutId;
public ToolBarHelper(Context context, int layoutId) {
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
/*初始化整个内容*/
initContentView();
/*初始化用户定义的布局*/
initUserView(layoutId);
/*初始化toolbar*/
initToolBar();
this.mLayoutId=layoutId;
}
private void initContentView() {
/*直接创建一个帧布局,作为视图容器的父容器*/
mContentView = new FrameLayout(mContext);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mContentView.setLayoutParams(params);
}
private void initToolBar() {
/*通过inflater获取toolbar的布局文件*/
View toolbar = mInflater.inflate(R.layout.toolbar, mContentView);
mToolBar = (Toolbar) toolbar.findViewById(R.id.id_tool_bar);
// mToolBar.setTitle("返回");
tv_toolbar_title = (TextView) toolbar.findViewById(R.id.tv_toolbar_title);
// mToolBar.inflateMenu(R.menu.toolbar_act_details);
}
@SuppressWarnings("ResourceType")
private void initUserView(int id) {
mUserView = mInflater.inflate(id, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);
/*获取主题中定义的悬浮标志*/
boolean overly = typedArray.getBoolean(0, false);
mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material);
/*获取主题中定义的toolbar的高度*/
int toolBarSize = (int) typedArray.getDimension(1, (int) 26);
typedArray.recycle();
/*如果是悬浮状态,则不需要设置间距*/
params.topMargin = overly ? 0 : toolBarSize;
mContentView.addView(mUserView, params);
}
public void setTvTitle(String text) {
tv_toolbar_title.setText(text);
}
public void setToolBarColor(@ColorInt int colorId) {
mToolBar.setBackgroundColor(colorId);
}
public FrameLayout getContentView() {
return mContentView;
}
public Toolbar getToolBar() {
return mToolBar;
}
}
toolbar.xml 这里写的tv_toolbar_title是一个居中的TextView,因为默认的toolbar.seTitle()设置的是偏左的标题
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent">
<!--设置高度为 ActionBar 的高度-->
<!--背景颜色为 ActionBar 的背景颜色-->
<!--返回按钮的图标-->
<!--android:background="?attr/colorPrimary"-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="5dp">
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_scrollFlags="scroll"
android:id="@+id/id_tool_bar"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator">
<TextView
android:id="@+id/tv_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="18sp"
android:layout_centerInParent="true"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</FrameLayout>
这2个方法来管理toolbar的颜色和标题
public void setTvTitle(String text) {
tv_toolbar_title.setText(text);
}
public void setToolBarColor(@ColorInt int colorId) {
mToolBar.setBackgroundColor(colorId);
}
根据需求定制自己的toolbar
public void initToolBarNew(String text, int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
mToolBarHelper.setTvTitle(text);
super.setTitle("");
setContentView(mToolBarHelper.getContentView());
toolbar = mToolBarHelper.getToolBar();
/*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar);
}
public void initToolBarNew(String text, @ColorInt int colorId, int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
mToolBarHelper.setTvTitle(text);
mToolBarHelper.setToolBarColor(colorId);
setStatusBarColor(colorId);
super.setTitle("");
setContentView(mToolBarHelper.getContentView());
toolbar = mToolBarHelper.getToolBar();
/*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar);
}
使用起来也很方便,只需要继承下ToolBarActivity 和initToolBarNew方法即可
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.toman.Adpter.RankAdapter;
import com.toman.R;
import com.toman.Util.PullZoomListView;
import com.toman.bean.RankBean;
import com.toman.toolbar.ToolBarActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class RankActivity extends ToolBarActivity {
@BindView(R.id.lv)
PullZoomListView lv;
private RankAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
initToolBarNew("OEE排行榜", getResources().getColor(R.color.myBgn),R.layout.activity_rank);
ButterKnife.bind(this);
initView();
initList();
}
private void initList() {
List<RankBean> list = new ArrayList<RankBean>();
for(int i=1;i<=20;i++){
RankBean bean=new RankBean(i,"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1522744818&di=fdc85a5e8f4c2466b32994fd89402af2&src=http://img.pconline.com.cn/images/upload/upc/tx/pcdlc/1612/07/c347/31720929_1481117981509.jpg","小"+i,i+"");
list.add(bean);
}
adapter.refreshData(list);
}
private void initView() {
lv.mHeaderImg.setImageResource(R.mipmap.img);
// lv.addHeaderView(headerView);
//mHeaderImage.setImageResource(R.mipmap.baselogo);
adapter = new RankAdapter(this);
lv.setAdapter(adapter);
}
}
希望能对大家有所帮助,有所启发,大家互相学习!
网友评论