自定义属性--理解

来源:互联网 发布:舆论是什么知乎 编辑:程序博客网 时间:2024/06/06 07:51

对自定义属性的简单理解:

①在资源目录下创建一个attrs xml文件

<declare-styleable name="test">        <attr name="demo" format="string" />        <attr name="demoAttr" format="integer" />    </declare-styleable>

②创建一个类继承view 并通过:TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.demo);来获取attrs下属性。代码:

 String text = ta.getString(R.styleable.test_demoAttr);        int textAttr = ta.getInteger(R.styleable.test_demo, -1);        ta.recycle();
③:吧自定义属性放在学校用到的xml文件中,如:
<span style="color: rgb(0, 102, 102); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">xmlns:zs</span><span style="color: rgb(0, 102, 102); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">=</span><span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"http://schemas.android.com/apk/res/com.example.test"</span>
<com.example.test.MyTextView        android:layout_width="100dp"        android:layout_height="200dp"        zs:testAttr="500"        zs:text="world" />

0 0
原创粉丝点击