继承RelativeLayout 自定义布局
public class HomeToolbarView extends RelativeLayout {
TextView tvTitle;
public HomeToolbarView(Context context) {
super(context);
addView(context);
}
public HomeToolbarView(Context context, AttributeSet attrs) {
super(context, attrs);
addView(context);
}
public HomeToolbarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addView(context);
}
public void addView(Context mContext){
ImageView imageViewMenu=new ImageView(mContext);
ImageView imageViewMessage=new ImageView(mContext);
tvTitle=new TextView(mContext);
addView(imageViewMenu);
addView(imageViewMessage);
addView(tvTitle);
RelativeLayout.LayoutParams menuLayoutParams = (RelativeLayout.LayoutParams) imageViewMenu.getLayoutParams();
RelativeLayout.LayoutParams messageLayoutParams = (RelativeLayout.LayoutParams) imageViewMessage.getLayoutParams();
RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams();
menuLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
menuLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
menuLayoutParams.setMargins(getResources().getDimensionPixelOffset(R.dimen.common_margin)
,0,0,0);
menuLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
menuLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
titleLayoutParams.width= LayoutParams.MATCH_PARENT;
titleLayoutParams.height=LayoutParams.MATCH_PARENT;
titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
messageLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
messageLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
messageLayoutParams.setMargins(0
,0,getResources().getDimensionPixelOffset(R.dimen.common_margin),0);
messageLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
messageLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
imageViewMenu.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_menu));
imageViewMessage.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_message));
tvTitle.setText(mContext.getString(R.string.app_name));
tvTitle.setTextSize(getResources().getDimension(R.dimen.textsize_8));
tvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.common_blue));
tvTitle.setGravity(Gravity.CENTER);
imageViewMenu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listener.leftClick();
}
});
imageViewMessage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listener.rightClick();
}
});
imageViewMenu.setLayoutParams(menuLayoutParams);
imageViewMessage.setLayoutParams(messageLayoutParams);
tvTitle.setLayoutParams(titleLayoutParams);
}
public void setTitle(String title){
tvTitle.setText(title);
}
//自定义的顶部ActionBar的点击监听;
private HomeToolbarClickListener listener;
//点击事件的监听接口
public interface HomeToolbarClickListener {
void leftClick();
void rightClick();
}
//提供activity调用的方法,类似于Button类的setOnClickListener(OnClickListener listener)
//传入具体实现方法
public void setOnTopbarClickListener(HomeToolbarClickListener listener){
this.listener=listener;
}
}
使用
<com.freexiaoyu.app.widget.HomeToolbarView
android:id="@id/toolbar_home"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="48.0dp"
android:orientation="vertical"/>
activity
@BindView(R.id.toolbar_home)
HomeToolbarView toolbar_home;
toolbar_home.setTitle(getString(R.string.app_name));
toolbar_home.setOnTopbarClickListener(new HomeToolbarView.HomeToolbarClickListener() {
@Override
public void leftClick() {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
@Override
public void rightClick() {
}
});
展示效果

继承RelativeLayout 自定义布局的更多相关文章
- Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)
前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...
- 深入解析_Android的自定义布局
前言: 这篇文章是前Firefox Android工程师(现在跳槽去Facebook了) Lucas Rocha所写,文中对Android中常用的四种自定义布局方案进行了很好地分析,并结合这四种And ...
- Android零基础入门第28节:轻松掌握RelativeLayout相对布局
原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...
- Collection View 自定义布局(custom flow layout)
Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...
- OC - 31.通过封装的自定义布局快速实现商品展示
概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...
- OC - 29.自定义布局实现瀑布流
概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...
- AcitonBar 自定义布局
Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 自定义Activity主题和ActionBar样式 在新建的 ...
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...
随机推荐
- 日常踩坑 — 相邻元素之间的margin合并问题。
踩坑:使用v-for渲染的组件,当然图中的id已经换成class,还是没有解决这个问题,于是各种查找资料,我就不信简单的CSS问题这么难解决! v-for渲染组件级传值: <div class= ...
- 从软件测试转型到C#上位机程序员
一直在做软件测试的工作,天天与程序员不依不饶的争论细节的问题,没想到自己也有那么一天走上程序员的道路,由此开始,我的博客天天更新自己的学习状态,分享自己的心得. C#是微软公司发布的一种面向对象的.运 ...
- IDEA Maven的下载和配置
首先去官网下载如图: 下载之后解压打开如图: 配置:1.打开conf文件夹下的settings.xml(我用的notepad++) 第55行左右加上图上的<localRepository> ...
- aiohttp AppRunner的用法
参考廖雪峰的aiohttp教程,会出现两个DeprecationWarning, loop argument is deprecated Application.make_handler(...) i ...
- urllib.error.URLError: <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
因为昨天我用fiddler抓包实验它的基本功能,今天运行程序时没有打开fiddler,所以配置的代理失效了,返回这样的错误. 这个问题是因为代理设置失效,换一个代理或者取消设置代理即可.
- height属性
高度属性: height:长度值|百分比|auto 最大高度:max-height 最小高度:min-height 说明:设置块级元素和替换元素的内容高度.
- Python入门 (一)
本文是个人python学习笔记,学习资料为廖雪峰python教程,如需更多内容,请移步廖老师官方网站. 一 安装 官网下载安装包安装,安装好之后,在命令提示符输入python进入Python交互模式: ...
- pycharm的快捷键
一.编辑(Editing) Ctrl+Space 基本的代码完成(类.方法.属性) Ctrl+Alt+Space 快速导入任意类 Ctrl+Shift+Enter 语句完成 Ctrl+P 参数信息(在 ...
- Ubuntu16.04中如何启用floodlight的一种方式
在 floodlight文件夹下输入 java -jar target/floodlight.jar 启动floodlight控制器,在浏览器窗口输入 http://localhost:8080/ui ...
- Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [33,755] milliseconds.
刚部署好程序,第一次登录时,加载非常得慢,查看log日志发现:Creation of SecureRandom instance for session ID generation using [SH ...