android--------自定义控件 之 组合控件篇
上篇介绍了自定义控件的自定义属性篇,地址:http://www.cnblogs.com/zhangqie/p/8969163.html
这篇博文主要来说说 自定义控件的组合控件来提高布局的复用
使用自定义组合控件的好处?
我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们就可以用自定义组合控件来实现,以提高开发效率,降低开发成本为导向的,也便于扩展。
当然也可以有其他方式,如 include 标签
1:标题栏布局文件
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView
android:id="@+id/title_tab_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:textSize="14sp"
/> <TextView
android:id="@+id/title_tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="1"
android:textSize="17sp" /> <Button
android:id="@+id/title_tab_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</merge>
2:属性文件
<declare-styleable name="TopTabToolView">
<attr name="tab_background_color" format="color"/>
<attr name="left_tab_visible" format="boolean"/>
<attr name="left_tab_drawable" format="reference|integer"/>
<attr name="title_text" format="string"/>
<attr name="title_color" format="color"/>
<attr name="right_tab_visible" format="boolean"/>
<attr name="right_tab_text" format="string"/>
<attr name="right_tab_text_color" format="color"/>
<attr name="right_tab_drawable" format="reference|integer"/>
</declare-styleable>
3:自定义控件
public class TopTabToolView extends RelativeLayout {
private ImageView titleBarLeftImg;
private Button titleBarRightBtn;
private TextView titleBarTitle;
public TopTabToolView(Context context) {
super(context);
}
public TopTabToolView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.tab_tool_layout,this,true);
titleBarLeftImg = (ImageView)findViewById(R.id.title_tab_left);
titleBarTitle = (TextView)findViewById(R.id.title_tab_title);
titleBarRightBtn = (Button)findViewById(R.id.title_tab_right);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TopTabToolView);
if (typedArray != null){
//背景设置
int titleBarBackGround = typedArray.getColor(R.styleable.TopTabToolView_tab_background_color, Color.WHITE);
setBackgroundColor(titleBarBackGround);
//-------------------------标题栏左边----------------------
boolean leftImgVisible = typedArray.getBoolean(R.styleable.TopTabToolView_left_tab_visible,true);
if (leftImgVisible){
titleBarLeftImg.setVisibility(VISIBLE);
}else {
titleBarLeftImg.setVisibility(GONE);
}
//设置图标
int leftTabDrawble = typedArray.getResourceId(R.styleable.TopTabToolView_left_tab_drawable,-1);
if (leftTabDrawble != -1){
titleBarLeftImg.setImageResource(leftTabDrawble);
}
//--------------------------中间标题-----------------------
String titleText = typedArray.getString(R.styleable.TopTabToolView_title_text);
if (!TextUtils.isEmpty(titleText)){
titleBarTitle.setText(titleText);
//设置字体颜色
int titleTextColor = typedArray.getColor(R.styleable.TopTabToolView_title_color,Color.WHITE);
titleBarTitle.setTextColor(titleTextColor);
}
//------------------------标题栏右边-------------------------
boolean rightButtonVisible = typedArray.getBoolean(R.styleable.TopTabToolView_right_tab_visible,true);
if (rightButtonVisible){
titleBarRightBtn.setVisibility(VISIBLE);
}else {
titleBarRightBtn.setVisibility(INVISIBLE);
}
//设置文字
String rightBtnText = typedArray.getString(R.styleable.TopTabToolView_right_tab_text);
if (!TextUtils.isEmpty(rightBtnText)){
titleBarRightBtn.setText(rightBtnText);
int rightBtnTextColor = typedArray.getColor(R.styleable.TopTabToolView_right_tab_text_color,Color.WHITE);
titleBarRightBtn.setTextColor(rightBtnTextColor);
}
//设置图标
int rightBtnDrawable = typedArray.getResourceId(R.styleable.TopTabToolView_right_tab_drawable,-1);
if (rightBtnDrawable != -1){
titleBarRightBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,rightBtnDrawable,0);
}
typedArray.recycle();
}
}
public TopTabToolView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 设置标题
* @param title
*/
public void setTitle(String title){
if (!TextUtils.isEmpty(title)){
titleBarTitle.setText(title);
}
}
/***
* 左边点击
* @param onClickListener
*/
public void setLeftOnClickListener(OnClickListener onClickListener){
if (onClickListener != null){
titleBarLeftImg.setOnClickListener(onClickListener);
}
}
/***
* 右边点击
* @param onClickListener
*/
public void setRightOnClickListener(OnClickListener onClickListener){
if (onClickListener != null){
titleBarRightBtn.setOnClickListener(onClickListener);
}
}
}
属性文件的设置也可以通过Java代码修改, 如: Title标题
4:Activity代码
public class Demo3Activity extends AppCompatActivity {
TopTabToolView topTabToolView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo3);
initView();
}
private void initView(){
topTabToolView = (TopTabToolView) findViewById(R.id.tab1);
topTabToolView.setTitle("代码设置标题");
topTabToolView.setLeftOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
topTabToolView.setRightOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Demo3Activity.this,"关闭",Toast.LENGTH_LONG).show();
}
});
}
}
效果图:

源码地址:https://github.com/DickyQie/android-custom-control
android--------自定义控件 之 组合控件篇的更多相关文章
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- Android自定义控件之日历控件
标签: android 控件 日历 应用 需求 2015年09月26日 22:21:54 25062人阅读 评论(109) 收藏 举报 分类: Android自定义控件系列(7) 版权声明:转载注 ...
- Flutter学习笔记(38)--自定义控件之组合控件
如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...
- android 自定义空间 组合控件中 TextView 不支持drawableLeft属性
android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
- 自定义控件之--组合控件(titlebar)
自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭. 组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程: 那么常见的组合控件有那些? 比如titl ...
- [Android学习笔记]组合控件的使用
组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...
- Android Studio自定义组合控件
在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
- android 自己定义组合控件
自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
随机推荐
- Eclipse使用maven命令安装第三方jar包
使用原因: 使用maven时,有些第三方jar包是不能从maven远程仓库中下载得到,因此导致在pom.xml中添加jar包依赖时会怎么添加都会报错(Missing artifact ojdbc:oj ...
- Bootstrap3基础 list-inline 无序列表横向显示
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 【系列教程1】Gradle入门系列三:依赖管理
在现实生活中,要创造一个没有任何外部依赖的应用程序并非不可能,但也是极具挑战的.这也是为什么依赖管理对于每个软件项目都是至关重要的一部分. 这篇教程主要讲述如何使用Gradle管理我们项目的依赖,我们 ...
- (zhuan) 一些RL的文献(及笔记)
一些RL的文献(及笔记) copy from: https://zhuanlan.zhihu.com/p/25770890 Introductions Introduction to reinfor ...
- (zhuan) 深度学习全网最全学习资料汇总之模型介绍篇
This blog from : http://weibo.com/ttarticle/p/show?id=2309351000224077630868614681&u=5070353058& ...
- 今天就整一个bug了
BeanPostProcessor加载次序及其对Bean造成的影响分析 SSM整合出现not found for dependency: expected at least 1 bean which ...
- Latex: 添加IEEE论文keywords
参考: How to use \IEEEkeywords Latex: 添加IEEE论文keywords 方法: \begin{IEEEkeywords} keyword1, keyword2. \e ...
- Execl矩阵如何转化成Pajek的net文件
在科研中我们有时会把把execl矩阵利用Ucinet.Pajek等可视化软件进行画图,而想要把execl矩阵转化为 Pajek可识别的文件-->net文件令很多初学者头疼不已,本文将做详细介绍. ...
- 在服务器端对sshd做白名单
1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...
- python中包的语法
1.什么是包? 包是一种通过".模块名"来组织python模块名称空间的方式.我们穿件的每个文件夹都可以被称为包. 但是要注意, 在python2中规定. 包内必须存在__init ...