自定义View之组合控件
来源:互联网 发布:程序员工作量化kpi 编辑:程序博客网 时间:2024/05/22 11:29
第一步:定义属性—-在res下的values目录下创建一个attrs.xml的文件,定义一些自己用到的属性
有关属性的详解
<resources> <declare-styleable name="TopBar"> <attr name="title" format="string"></attr> <attr name="titleTextSize" format="dimension"></attr> <attr name="titleTextColor" format="color"></attr> <attr name="leftTextColor" format="color"></attr> <attr name="leftBackground" format="reference|color"></attr> <attr name="leftText" format="string"></attr> <attr name="rightTextColor" format="color"></attr> <attr name="rightBackground" format="reference|color"></attr> <attr name="rightText" format="string"></attr> </declare-styleable></resources>
定义完属性后,新建一个自定义的类继承ViewGroup.这里继承至RelativeLayout
/** * 组合控件 */public class CompoundControl extends RelativeLayout{ private topbarClickListener topbarClickListener; private Button leftButton,rightButton; private TextView mTextTitle; private String leftText,mRightText,mTitleText; private int leftTextColor,mRightTextColor,mTitleTextColor; private float mTitleTextSize; private Drawable mRightBackground,mLeftBackground; private LayoutParams LeftlayoutParams,rightLayoutParams,TitleLayoutParams; public CompoundControl(Context context) { super(context); } public CompoundControl(Context context, AttributeSet attrs) { super(context, attrs); init(context,attrs);//记得调用初始化 } public CompoundControl(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs);//记得调用初始化 } /** * 获取自定义的属性 设置属性参数 * @param context * @param attrs */ private void init(Context context,AttributeSet attrs){ /** * 第一步引用自定义属性 */ TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.TopBar); /** * 赋值 */ //左边的按钮 leftTextColor = array.getColor(R.styleable.TopBar_leftTextColor,0); leftText=array.getString(R.styleable.TopBar_leftText); mLeftBackground=array.getDrawable(R.styleable.TopBar_leftBackground); //右边的 mRightBackground=array.getDrawable(R.styleable.TopBar_rightBackground); mRightText=array.getString(R.styleable.TopBar_rightText); mRightTextColor=array.getColor(R.styleable.TopBar_rightTextColor,0); //标题 mTitleText=array.getString(R.styleable.TopBar_title); mTitleTextColor=array.getColor(R.styleable.TopBar_titleTextColor,0); mTitleTextSize=array.getDimension(R.styleable.TopBar_titleTextSize,10); /** * 记得调用recycler避免重新创建的时候错误 */ array.recycle();//回收 /** * 自定义三个控件 */ leftButton=new Button(context); rightButton=new Button(context); mTextTitle=new TextView(context); leftButton.setText(leftText); leftButton.setTextColor(leftTextColor); leftButton.setBackground(mLeftBackground); rightButton.setText(mRightText); mTextTitle.setText(mTitleText); mTextTitle.setTextColor(mTitleTextColor); mTextTitle.setTextSize(mTitleTextSize); mTextTitle.setGravity(Gravity.CENTER); /** * 设置控件的大小 */ LeftlayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); LeftlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); /** * 添加到ViewGroup */ addView(leftButton,LeftlayoutParams); rightLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); addView(rightButton,rightLayoutParams); TitleLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); TitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(mTextTitle,TitleLayoutParams); setOnClickListener(); } /** * 设置点击事件 我在这里只写了左边的点击事件 * 自定义一个接口 通过映射接口去调用实现方法 */ private void setOnClickListener(){ leftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { topbarClickListener.leftClick(); } }); } /** * 给外部提供一个调用的方法 * @param on */ public void setTopbarClickListener(topbarClickListener on ){ this.topbarClickListener=on; }
点击事件接口
public interface topbarClickListener { void leftClick(); void rightClick();}
引用自定义的类—注意名字空间 xmlns:cumtor可以随便起名
<wifi.yeeloor.com.scoller.CompoundControl xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cumtor="http://schemas.android.com/apk/res-auto" android:id="@+id/topBar" cumtor:leftText="Back" cumtor:leftTextColor="#000000" cumtor:title="自定义标题" cumtor:titleTextSize="15sp" cumtor:titleTextColor="#123412" cumtor:rightText="More" android:layout_width="match_parent" android:layout_height="40dp"></wifi.yeeloor.com.scoller.CompoundControl><!--记得属性必须全写-->
然后通过引用<include>
调用到相应的位置
<include layout="@layout/topbar" android:id="@+id/mainTopbar"></include>
0 0
- 自定义View之组合控件
- 自定义View之组合控件
- 自定义View之组合控件
- 自定义View之组合控件
- 自定义View之组合自定义控件
- 自定义View组合控件
- 自定义View---自定义组合控件
- Android 自定义组合控件View
- 自定义view组合控件笔记
- android:自定义view--组合控件
- [自定义view]Android实战技巧之组合控件
- 【安卓笔记】自定义view之组合控件
- Android自定义View之组合控件 ---- LED数字时钟
- Android自定义View之组合控件-仿电商app顶部栏
- Android自定义View 自定义组合控件
- Android自定义View----1. 自定义组合控件
- 关于自定义组合控件以及自定义view
- 自定义控件之组合控件
- solr入门之拼音加汉字方式的搜索建议自动补全的不高效实现
- Ubuntu下面包依赖损坏的解决unmet dependencies
- AFNetworking 3.0的基本使用(自己做项目总结的)
- Facebook面试题 check for subText in input text's subarray.
- Remote Notification (一)
- 自定义View之组合控件
- NoSQL 数据库概览及其与 SQL 语法的比较
- ios AFNetworking支持https请求
- [绍棠] Xcode编译遇到bitcode问题
- Tomcat 配置详解及优化
- Android 多种方式正确的加载图像,有效避免oom
- 在Eclipse中使用Git
- solr spell拼写检查
- Code Sign Error in macOS Sierra Xcode 8 : resource fork, Finder information