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程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
随机推荐
- Vim 学习
主要分为三种模式: 一般模式 编辑模式 命令行模式 光标的移动 单词级 比单纯的逐个字符的移动,效率要高 w or W 向移动到下一单词开头 ★★ b or B 向左移动到单词开头 ★★ 块级 gg文 ...
- openwrt支持哪些c库?
答: 至2019/3/22,支持两种,一种是glibc,另一种是musl-libc(openwrt默认使用musl-libc)
- MySql 语句收集
目录 =与:=区别 序列号: 分组: 子查询分组: 同数据库表数据迁移 存储过程 参考: =与:=区别 = 只有在set和update时才是和:=一样,赋值的作用,其它都是等于的作用.鉴于此,用变量实 ...
- Tutorial on word2vector using GloVe and Word2Vec
Tutorial on word2vector using GloVe and Word2Vec 2018-05-04 10:02:53 Some Important Reference Pages ...
- (转) Learning Deep Learning with Keras
Learning Deep Learning with Keras Piotr Migdał - blog Projects Articles Publications Resume About Ph ...
- AndroidStudio Gradle下载速度慢解决方法
1.在软件里点开工程文件下的 build.gradle 2..在 buildscript 和 allprojects 的 repositories 中分别注释掉 jcenter() 3.在 build ...
- printf和std::cout ...endl
printf效率要比std::cout...endl高些,可以减少打印所花时间
- 17秋 SDN课程 第二次上机作业
1.控制器floodlight所示可视化图形拓扑的截图,及主机拓扑连通性检测截图 拓扑 连通性 2.利用字符界面下发流表,使得'h1'和'h2' ping 不通 流表截图 连通性 3.利用字符界面下发 ...
- Gym 100247I Meteor Flow(优先队列)
https://vjudge.net/problem/Gym-100247I 题意:有一艘飞船,现在有n颗流星坠落会攻击到飞船,每颗流星会在t时刻降落,对飞船造成d的伤害,飞船会有一个保护盾,初始值为 ...
- RN中关于IOS和Android的相关权限的问题
在日常的开发中,时常需要去获取应用的一权限 比如查看通讯录/打开摄像机等 1:ios iOS 的权限管理在info.plist里设置 info.plist主要是管理了app 的一些信息文件,比如版本 ...