ToolBar的封装
2016-03-23 12:26
477 查看
1.ToolBarHelper.java
这个类的功能是:先创建一个 ViewGroup 来作为视图的父 View,把用户定义的 View,和 toolBar 依次 Add 到 ViewGroup 中;
2.ToolBarActivity.java
这个类的功能是:先创建一个 ViewGroup 来作为视图的父 View,把用户定义的 View,和 toolBar 依次 Add 到 ViewGroup 中;
public class ToolBarHelper { private Context mContext; private FrameLayout mContentView; /*用户定义的view*/ private View mUserView; /*toolbar*/ private Toolbar mToolBar; /*视图构造器*/ private LayoutInflater mInflater; /* * 两个属性 * 1、toolbar是否悬浮在窗口之上 * 2、toolbar的高度获取 * */ private static int[] ATTRS = { R.attr.windowActionBarOverlay, R.attr.actionBarSize }; public ToolBarHelper(Context context, int layoutId) { this.mContext = context; mInflater = LayoutInflater.from(mContext); /*初始化整个内容*/ initContentView(); /*初始化用户定义的布局*/ initUserView(layoutId); /*初始化toolbar*/ initToolBar(); } 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); } 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); /*获取主题中定义的toolbar的高度*/ int toolBarSize = (int) typedArray.getDimension(1, (int) mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material)); typedArray.recycle(); /*如果是悬浮状态,则不需要设置间距*/ params.topMargin = overly ? 0 : toolBarSize; mContentView.addView(mUserView, params); } public FrameLayout getContentView() { return mContentView; } public Toolbar getToolBar() { return mToolBar; } }
2.ToolBarActivity.java
public abstract class ToolBarActivity extends AppCompatActivity { private ToolBarHelper mToolBarHelper; private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void setContentView(int layoutResID) { mToolBarHelper = new ToolBarHelper(this, layoutResID); toolbar = mToolBarHelper.getToolBar(); setContentView(mToolBarHelper.getContentView()); /*把 toolbar 设置到Activity 中*/ setSupportActionBar(toolbar); /*自定义的一些操作*/ onCreateCustomToolBar(toolbar); } public void onCreateCustomToolBar(Toolbar toolbar) { toolbar.setContentInsetsRelative(0, 0); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } }
相关文章推荐
- OpenCV学习笔记(二十八)——光流法对运动目标跟踪Video
- Python debug(二)
- 02 - 加载XIB(ViewController)后 View的子控件无响应中
- UDP的特点以及使用案例
- Linear time ordering
- WebView加载html页面
- 夜间模式的切换
- HTML网页之打地鼠游戏
- UDP的使用方法
- javascript 方法的书写
- httpClient(Get)进行网络请求
- 01 - Pushing a navigation controller is not supported
- 利用网络进行文件的上传,通过客户端上传到服务端
- C#委托(delegate与事件(Event)
- 解析增量xml数据到数据库
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- 带命名空间的XML的dom4j应用<转>
- win7安装svn后相关问题
- HttpClient工具类v1.7
- HTML网页之坦克大战游戏