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程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
随机推荐
- 2015,3,10 2(南阳理工ACM)
描述有一个整型偶数n(2<= n <=10000),你要做的是:先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出. 输入 第一行有一个整数i(2<=i<30) ...
- 三元运算符代替if-else
main(List<String> args) { int age = 60; String status = age < 50 ? "Still young" ...
- 更新32位Spyder从3.0.0-> 3.2.3
https://stackoverflow.com/questions/51222550/how-to-update-spyder-3-3-0 It works!! 1. went to the An ...
- 【分库、分表】MySQL分库分表方案
一.Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. ...
- UVA11417 GCD
题目地址 题目链接 题解 先讨论任何没有限制的情况 \[ \large { \begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j)\\ &a ...
- Eclipse和Tomcat使用过程的一些配置、错误等的总结记录
背景:公司项目使用jdk1.6.tomcat7.SVN,本文总结使用到现在的一些配置和问题. 1.Eclipse项目几点配置:(1)Windows -> Preferences -> Ja ...
- Hive command
hive常用命令 Hadoop Hive概念学习系列之hive里的分区(九) DOC hive分区(partition)简介 Hive分区(静态分区+动态分区) Hive分区.分桶操作及其比较 hiv ...
- 【AT1219】历史研究
Problem Description \(IOI\)国历史研究的第一人--\(JOI\)教授,最近获得了一份被认为是古代\(IOI\)国的住民写下的日记.\(JOI\)教授为了通过这份日记来研究古代 ...
- jQuery 知识点总结
jQuery 是一个“写的更少,但做的更多”的轻量级JavaScript 库.对于网页开发者来说,学会jQuery是必要的.因为它让你了解业界最通用的技术,为将来学习更高级的库打下基础,并且确实可以很 ...
- Python 爬起数据时 'gbk' codec can't encode character '\xa0' 的问题
1.被这个问题折腾了一上午终于解决了,再网上看到有用 string.replace(u'\xa0',u' ') 替换成空格的,方法试了没用. 后来发现 要在open的时候加utf-8才解决问题. 以 ...