上篇介绍了自定义控件的自定义属性篇,地址: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--------自定义控件 之 组合控件篇的更多相关文章

  1. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  2. Android自定义控件之日历控件

      标签: android 控件 日历 应用 需求 2015年09月26日 22:21:54 25062人阅读 评论(109) 收藏 举报 分类: Android自定义控件系列(7) 版权声明:转载注 ...

  3. Flutter学习笔记(38)--自定义控件之组合控件

    如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...

  4. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  5. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  6. 自定义控件之--组合控件(titlebar)

    自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭.   组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程: 那么常见的组合控件有那些? 比如titl ...

  7. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

  8. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

  9. android 自己定义组合控件

    自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...

随机推荐

  1. ODAC(V9.5.15) 学习笔记(三)TOraSession(1)

    1. 连接相关 名称 类型 说明 ConnectDialog 执行连接对话框控件 Connected Boolean 连接状态,通过函数Connect和Disconnect连接或关闭数据库连接,并触发 ...

  2. DataSnap下的分包获取

    DataSnap下通过TQuery—TDataSetProvider—TClientDataSet获取数据,如果是主从数据,则每条主表记录都会触发从表数据的获取. 这种获取和组织数据的方式有一个问题: ...

  3. luogu P2486 [SDOI2011]染色

    树剖做法: 就是两个dfs+一个线段树 难度的取决基本==线段树的维护难度 所以对有点线段树基础的,树剖也不难做吧 这里操作有二 一:两点间路径染色 线段树的区间赋值操作 二:查询路径段的个数 考虑线 ...

  4. js插入排序

    插入排序 平均时间复杂度O(n*n) 最差情况O(n*n) 最好情况O(n) 空间复杂度O(1) 稳定性:稳定 function insertSort (arr) { var len = arr.le ...

  5. [exceltolist] - 一个excel转list的工具

    https://github.com/deadzq/cp-utils-excelreader  <(感谢知名网友的帮助) https://sargeraswang.com/blog/2018/1 ...

  6. (转载)C#:Form1_Load()不被执行的三个解决方法

    我的第一个c#练习程序,果然又出现问题了...在Form1_Load() not work.估计我的人品又出现问题了. 下面实现的功能很简单,就是声明一个label1然后,把它初始化赋值为hello, ...

  7. Tomcat服务器环境变量配置及在Eclipse中启动和配置

    本文原创,转载需注明出处: 如何配置在Eclipse中配置Tomcat服务器 1.在配置的时候要右击‘我的电脑‘看是否安装了jdk,配置了jdk的环境变量,看是否有classpath和path是否指向 ...

  8. BZOJ 2754 【SCOI2012】 喵星球上的点名

    题目链接:喵星球上的点名 首先可以发现姓和名两个串就是逗你玩的.在两个串中间插入一个\(10001\),当成一个串做就可以了. 于是我们的问题转化为了: 有\(n\)个串\(A_1,A_2,\dots ...

  9. FreeCodeCamp---advanced Algorithm Scripting解法

    Exact Change 设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) ...

  10. eureka 和zookeeper 区别 优势【转】

    作为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的, ...