declare-styleable的使用

declare-styleable的使用declare-styleable的使用-carlosk-博客园declare-styleable是给自定义控件添加自定义属性用的1.首先,先写attrs.xml…

大家好,又见面了,我是你们的朋友全栈君。

declare-styleable的使用 – carlosk – 博客园

declare-styleable是给自定义控件添加自定义属性用的

1.首先,先写attrs.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TestAttr">
        <attr name="name" format="reference" />
        <attr name="age">
            <flag name="child" value="10" />
            <flag name="young" value="18" />
            <flag name="oldman" value="60" />
        </attr>
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>
复制代码

reference指的是是从string.xml引用过来
flag是自己定义的,类似于 android:gravity="top"
dimension 指的是是从dimension.xml里引用过来的内容.注意,这里如果是dp那就会做像素转换

2.在布局文件里的写法
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >s

    <com.arlos.attrstest.MyTestView
        android:id="@+id/tvTest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         attrstest:name="@string/myname"
         android:gravity="top"
         attrstest:age="young"
         attrstest:textSize="@dimen/aa"
        android:text="@string/hello" />

</LinearLayout>
复制代码

    2.1 先引用这个dtd

xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"
attrstest是随便写的.后面的包名是你所在的项目的根包.也就是在manifest里的com.arlos.attrstest

 2.2 在自定义的控件里写属性

3. 最后在控件的构造方法里取得这些值
复制代码
public class MyTestView extends TextView {

    public MyTestView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray tArray = context.obtainStyledAttributes(attrs,
                R.styleable.TestAttr);
        String name = tArray.getString(R.styleable.TestAttr_name);
        System.out.println("name = " + name);
        int age = tArray.getInt(R.styleable.TestAttr_age, 200);
        System.out.println("age = " + age);
         float demin = tArray.getDimension(R.styleable.TestAttr_textSize,0);
         System.out.println("demin = " + demin);
        tArray.recycle();
    }
}
复制代码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154529.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机445 135 139端口,Win7如何关闭445 135 138 139端口[通俗易懂]

    计算机445 135 139端口,Win7如何关闭445 135 138 139端口[通俗易懂]5月12日全球爆发勒索病毒,国内外很多行业已遭受勒索病毒的侵害,勒索病毒迅速传播,目前已知传播途径基本都是通过445135137138139端口进行传播,那么只要关闭这些端口即可起到很好的预防作用,本文将介绍Win7怎么关闭445135137138139端口的方法。关闭445135137138139端口方法教程:1、打开Windows徽标(开始菜单),点击“控制面板”;2、…

    2022年10月10日
    3
  • 报错注入详解_报错注入的过程

    报错注入详解_报错注入的过程报错注入报错注入是SQL注入的一种。利用前提:页面上没有显示位,但是需要输出SQL语句执行错误信息。比如mysql_error()优点:不需要显示位缺点:需要输出mysql_error()的报错信息报错函数1、floor报错注入floor()报错注入是利用count()、rand()、floor()、groupby这几个特定的函数结合在一起产生的注入漏洞,准确的说是floor,count,groupby冲突报错。报错原理:利用数据库表主键不能重复的原理,使用GROUPBY分组,产生主

    2022年9月30日
    3
  • 激光slam综述_激光点云处理

    激光slam综述_激光点云处理1:SLAM是什么SLAM是同步定位与地图构建(SimultaneousLocalizationAndMapping)的缩写,最早由HughDurrant-Whyte和JohnJ.Leonard提出。SLAM主要用于解决移动机器人在未知环境中运行时定位导航与地图构建的问题。SLAM通常包括如下几个部分,特征提取,数据关联,状态估计,状态更新以及特征更新等。其中包括2D-SLAM和3D-SLAM。一下大概分为三种形式:Localization:在给定地图的情况下,估计机器人的位姿。SLA

    2022年8月23日
    8
  • 4种kill某个用户所有进程的方法

    4种kill某个用户所有进程的方法

    2021年7月8日
    208
  • 《github一天,一个算术题》:堆算法接口(堆排序、堆插入和堆垛机最大的价值,并删除)

    《github一天,一个算术题》:堆算法接口(堆排序、堆插入和堆垛机最大的价值,并删除)

    2022年1月10日
    45
  • Django之模板继承

    为了在Django模板配置中减少代码的冗余,需使用模板继承1.语法2.步骤(1)创建一个base.html把需要显示的页面内容写在里面(2)在title部分写个盒子,以后谁来扩展就在这里面

    2021年12月29日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号