为自定View添加相应属性,在XML可以直接使用
来源:互联网 发布:多目标 优化 编辑:程序博客网 时间:2024/05/07 05:00
首先在values下新建一个atts文件夹,添加
declare-styleable
<?xml version="1.0" encoding="utf-8"?><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="color|reference"></attr> <attr name="leftText" format="string"></attr> <attr name="rightText" format="string"></attr> <attr name="rightBackground" format="color|reference"></attr> <attr name="rightTextColor" format="color"></attr> </declare-styleable></resources>
XML布局
<?xml version="1.0" encoding="utf-8"?><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.hc.mytopbar.TopBar android:id="@+id/tp" android:layout_width="wrap_content" android:layout_height="wrap_content" custom:leftText="back" custom:leftTextColor="#ffff00" custom:rightTextColor="#ffff00" custom:rightText="menu" custom:title="菜单" custom:titleTextColor="#123412" custom:titleTextSize="10sp"> </com.example.hc.mytopbar.TopBar></RelativeLayout>
然后在自定义View类中的构造函数中 通过TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); 获得这个atts文件
获得atts中自定义属性之后 将属性与相应的控件绑定public class TopBar extends RelativeLayout { private Button leftButton, rightButton; private TextView tv_title; private int leftTextColor; private Drawable leftBackground; private String leftText; private int rightTextColor; private Drawable rightBackground; private String rightText; private float titleTextSize; private int titleTextColor; private String title; private LayoutParams leftParams, rightParams, titleParam; private topbarClickListener listener; public interface topbarClickListener { public void leftClick(); public void rightClick(); } public void setOnTopbarClickListener(topbarClickListener listner) { this.listener = listner; } public TopBar(final Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground); leftText = ta.getString(R.styleable.Topbar_leftText); rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0); rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground); rightText = ta.getString(R.styleable.Topbar_rightText); titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0); title = ta.getString(R.styleable.Topbar_title); ta.recycle(); setBackgroundColor(0xfff59563); leftButton = new Button(context); rightButton = new Button(context); tv_title = new TextView(context); leftButton.setTextColor(leftTextColor); leftButton.setText(leftText); leftButton.setBackground(leftBackground); rightButton.setText(rightText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); tv_title.setTextColor(titleTextColor); tv_title.setTextSize(titleTextSize); tv_title.setText(title); tv_title.setGravity(Gravity.CENTER); leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); leftParams.addRule(ALIGN_PARENT_LEFT, TRUE); addView(leftButton, leftParams); rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); rightParams.addRule(ALIGN_PARENT_RIGHT, TRUE); addView(rightButton, rightParams); titleParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); titleParam.addRule(CENTER_IN_PARENT); addView(tv_title, titleParam);
阅读全文
0 0
- 为自定View添加相应属性,在XML可以直接使用
- 如何在xml中直接添加view点击监听
- 为自定义View在xml里面设置属性
- 为自定义View添加属性
- android 自定属性的使用
- 如何自定View
- 自定view
- 自定view画圆
- WPF 在ResourceDictionary文件中使用自定的属性作为trigger 的target
- android中一个自定属性的xml错误
- 在maven的pom.xml中添加<dependency>直接可以下载jar包吗
- 为form指定target属性值,就可以使跳转页面在相应的frameset中显示
- 自定属性
- android 自定义view添加自定义xml属性
- 给自定义View添加xml属性
- 自定义View以及View的属性的使用(重点,xml中定义属性的值),包括Bitmap存为jpg样式(可以模仿修改照片不喜欢的地方)
- [Windows7]在开始菜单右侧添加一个自定义项
- 自定view(一)
- MRI数据格式转换——基于Nipype/FreeSurfer
- 自定义WinForm控件无法拖动到窗口
- 服务器常用的状态码及其对应的含义
- Linux-Ubuntu入门笔记
- redis总结1-Redis简介、安装、集群
- 为自定View添加相应属性,在XML可以直接使用
- Java 多项式求值
- python os.stat() 和 stat模块详解
- node.js学习笔记2——querystring (查询字符串,参数处理)
- Git高频命令(长期更新)
- C语言指针学习笔记
- 数据结构编程笔记十七:第六章 树和二叉树 赫夫曼树的实现
- 史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView
- requests库入门-5-带参数的请求类型