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 自定义布局的更多相关文章

  1. Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)

    前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...

  2. 深入解析_Android的自定义布局

    前言: 这篇文章是前Firefox Android工程师(现在跳槽去Facebook了) Lucas Rocha所写,文中对Android中常用的四种自定义布局方案进行了很好地分析,并结合这四种And ...

  3. Android零基础入门第28节:轻松掌握RelativeLayout相对布局

    原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...

  4. Collection View 自定义布局(custom flow layout)

    Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...

  5. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

  6. OC - 31.通过封装的自定义布局快速实现商品展示

    概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...

  7. OC - 29.自定义布局实现瀑布流

    概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...

  8. AcitonBar 自定义布局

    Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 自定义Activity主题和ActionBar样式 在新建的 ...

  9. Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面

    网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...

随机推荐

  1. Linux 驱动——Led驱动2

    led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...

  2. python爬虫,使用BeautifulSoup解析爬出来的HTML代码时报错

    UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for thi ...

  3. 【linux】State : Uninterruptible, pid: 3936

    运行centos7中yum命令时提示: Existing lock /var/run/yum.pid: another copy is running as pid 3936.Another app ...

  4. 解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during ...HRESULT: 0x80070422(注意尾部的错误号)

    解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during . ...

  5. Git学习笔记--命令

    git init--初始化Git仓库 git add <fils>--将文件添加到暂存区,可添加多个文件,空格隔开 git commit--提交到仓库 git status--查看工作区状 ...

  6. 项目启动失败,异常代码(StandardEngine[Catalina].StandardHost[localhost].StandardContext[/credit]]) ,dataSource 也报错

    问题:tomcat 项目启动失败(有多个springboot项目)! 28-Apr-2019 12:01:12.162 严重 [localhost-startStop-1] org.apache.ca ...

  7. 创建Windows服务

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

  8. SQLI DUMB SERIES-16

    (1)无论怎么输入,都没有回显,但当输入 admin")#时,显示登录成功.若通过其他方法获取数据库的用户名,可通过这个方法登录成功. (2)获取其他信息可用考虑时间延迟注入.方法同15.

  9. Matlab rgb2hsv

    >> im = imread('lake.jpg');>> imshow(im)>> hsv_im = rgb2hsv(im);>> imshow(hs ...

  10. PTA3

    一.7-1 抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置 ...