为自定View添加相应属性,在XML可以直接使用
2017-08-26 23:45
441 查看
首先在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[b]: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);
相关文章推荐
- 自定义View以及View的属性的使用(重点,xml中定义属性的值),包括Bitmap存为jpg样式(可以模仿修改照片不喜欢的地方)
- 使用DTD文档对 xml 1 作业中的xml文档进行约束,其中河北省只能依次包含石家庄市、张家口市、保定市这三个市,保定市可以有多个或者1个,给保定市定义属性编号为ID类型,必须有;属性name为字符
- 如何在xml中直接添加view点击监听
- Android至ViewPager添加切换动画——使用属性动画
- xml中不能直接添加ViewGroup吗
- c# 使用datagridview 浏览shp图层属性表(是否可以在这里直接修改呢?有待研究)
- XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
- 为DataGrid的行添加属性,使其可以使用onmouseover的javascript
- c++builder还有个好处,可以直接添加pas到工程中,混合使用.
- Android 图片轮番 继承ViewGroup实现 可以直接使用
- 利用运行时,给UIImageView写一个分类,交换里面的setImage的方法,可以重绘图片,提高内存的利用率(要是没有重绘图片,直接使用系统提供的setImag就会造成占用大量的内存问题)
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- iOS 使用 Runtime Attribute 为 IB 控件添加不能直接设置的属性
- Android 属性动画(Property Animation 给按钮的添加添加动画效果,同样可以用在整个布局上) (下)(viewGroup创建对象竟然可以用布局的id)
- XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
- 给自定义View添加xml属性
- 使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中。添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)
- Android自定义View中的自定义属性(attrs.xml,TypedArray的使用)
- XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
- android 自定义view添加自定义xml属性