android复合控件
<resources>
<declare-styleable name="TopBar">
<!--<attr name="android:title"/>-->
<attr name="title1" format="string" />
<attr name="titleTextSize1" format="dimension" />
<attr name="titleTextColor1" format="color" />
<attr name="leftTextColor" format="color" />
<attr name="leftBackground" format="reference|color" />
<attr name="leftText" format="string" />
<attr name="rightTextColor" format="color" />
<attr name="rightBackground" format="reference|color" />
<attr name="rightText" format="string" />
</declare-styleable>
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
//2.从TypedArray中取出对应的值来为要设置的属性赋值。
String mTitle = ta.getString(R.styleable.TopBar_title1);
float mTitleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize1, 10);
int mTitleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor1, 0);
String mLeftText = ta.getString(R.styleable.TopBar_leftText);
Drawable mLeftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
int mLeftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
String mRightText = ta.getString(R.styleable.TopBar_rightText);
Drawable mRightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
int mRightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
//3.获取完TypedArray的值后,调用recycle进行资源的回收,避免重新创建的时候的错误。
mLeftButton = new Button(context);
mRightButton = new Button(context);
mTitleView = new TextView(context);
//2.为控件赋值(自定义的属性)
mLeftButton.setText(mLeftText);
mLeftButton.setBackground(mLeftBackground);
mLeftButton.setTextColor(mLeftTextColor);
mRightButton.setText(mRightText);
mRightButton.setBackground(mRightBackground);
mRightButton.setTextColor(mRightTextColor);
mTitleView.setText(mTitle);
mTitleView.setTextSize(mTitleTextSize);
mTitleView.setTextColor(mTitleTextColor);
mTitleView.setGravity(Gravity.CENTER);
//3.设置布局
LayoutParams mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(mLeftButton, mLeftParams);
LayoutParams mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(mRightButton, mRightParams);
LayoutParams mTitleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);;
@Override
public void onClick(View v) {
mListener.leftClick();
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.rightClick();
}
});
@Override
public void leftClick() {
Toast.makeText(MainActivity.this,"点击左边按钮",Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this,"点击右边按钮",Toast.LENGTH_SHORT).show();
}
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.geyan.textviewdemo.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftBackground="#ffa4c161"
custom:leftText="Back"
custom:leftTextColor="#FFFFFF"
custom:rightBackground="#ffa4c161"
custom:rightText="More"
custom:rightTextColor="#FFFFFF"
custom:title1="自定义标题"
custom:titleTextColor1="#000000"
custom:titleTextSize1="8sp"></com.example.geyan.textviewdemo.TopBar>
</RelativeLayout>
android复合控件的更多相关文章
- android:layout_margin真实含义 及 自己定义复合控件 layout()运行无效的问题解决
一.关于layout_margin 搞Android时间也不短了.对layout_margin也不陌生了,可近期遇到一个问题让我发现,对它的认识还不够深入全面.大量网络资料上都说,layout_mar ...
- 介绍几个工作开发中封装的好用的android自定义控件
首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionE ...
- 怎样进行Android UI元素设计
Android UI元素里面包含了许多的内容,比如:该平台由操作系统.中间件.用户界面和应用软件组成,一个应用程序要想受用户喜爱,那么UI可不能差. Android为相似的编程名词引入了一些新的术语, ...
- 《Android群英传》读书笔记 (2) 第三章 控件架构与自定义控件详解 + 第四章 ListView使用技巧 + 第五章 Scroll分析
第三章 Android控件架构与自定义控件详解 1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWin ...
- 我的Android 4 学习系列之创建用户基本界面
目录 使用视图和布局 理解Fragment 优化布局 创建分辨率无关的用户界面 扩展.分组.创建和使用视图 使用适配器将数据绑定到视图 使用视图和布局 1. Android UI 几个基本概念 视图: ...
- 介绍几个好用的android自定义控件
首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionE ...
- Android群英传笔记——第三章:Android控件架构与自定义控件讲解
Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...
- Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!
Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...
- 细说Android事件传递
一.View的dispatchTouchEvent和onTouchEvent 探讨Android事件传递机制前,明确android的两大基础控件类型:View和ViewGroup.View即普通的控件 ...
随机推荐
- 关于python的import
在软件包里,必须添加__init__.py文件. 想要对外公开的module必须在__init__.py内import一次,这样这些module才能被外部代码import并调用.
- Codeforces 367
A. Sereja and Algorithm 水题不解释. B. Sereja ans Anagrams 模p同余的为一组,随便搞. C. Sereja and the Arrangement of ...
- RabbitMQ (三) 发布/订阅 -摘自网络
这篇博客中,我们会做一些改变,就是把一个消息发给多个消费者,这种模式称之为发布/订阅(类似观察者模式). 为了验证这种模式,我们准备构建一个简单的日志系统.这个系统包含两类程序,一类程序发动日志,另一 ...
- NetAddr
http://www.searchdatabase.com.cn/showcontent_66349.htm [techTarget中国,其专注于IT领域企业级高端市场,为IT专业技术人员和管理决 ...
- 第三百零六天 how can I 坚持
今天做了件并不是我风格的事,送了张公交卡,还没送出去,好难搞啊.这天会铭记的.如果将来我们能走在一起. 中午去朝阳门拿了我的荣事达破壁机,好大啊,怎么带回家啊,还有,回家要不要买两只烤鸭啊. 今天聊了 ...
- Spark RDD概念学习系列之Spark的算子的分类(十一)
Spark的算子的分类 从大方向来说,Spark 算子大致可以分为以下两类: 1)Transformation 变换/转换算子:这种变换并不触发提交作业,完成作业中间过程处理. Transformat ...
- work7
uno. 理解C++变量的作用域和生命周期 没有要求讲解我就简单注释了一下~ #include <iostream>int main(){ for (int i=0;i<10;i++ ...
- http://www.hameister.org/JavaFX_PuzzleGame.html
http://www.hameister.org/JavaFX_PuzzleGame.html
- DATASNAP 自增长字段问题
mssql数据表中包含有自动增值字段khid,类型为identify(1,1),且为表的主关键字; 在程序中修改和删除都没有问题,但增行时,增行总是提示错误'key violation'! 如何取消这 ...
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...