zoukankan      html  css  js  c++  java
  • 自定义组件使用属性资源

    在 res/values目录下的attrs.xml中定义资源

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="duration"></attr>
        <declare-styleable name="myButton">
            <attr name="duration"></attr>
        </declare-styleable>
    </resources>

    定义一个自定义组件

    public class MyButton extends Button
    {
        int duration;
        public  MyButton(Context context,AttributeSet attr)
        {
            super(context,attr);
    
            TypedArray typedArray=context.obtainStyledAttributes(attr,R.styleable.myButton );
             duration=typedArray.getInt(R.styleable.myButton_duration,0);
            this.setText(duration+"duration");
        }
    }
    

    在布局文件中使用自定义组件,记得设置好命名空间,一开始res-auto部分按照网上的说法应该是自定义组件所在的包名,but我使用是无线错,然后神奇发现可以这样

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:my="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
       >
    
        <com.example.my.MyButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            my:duration="10"/>
    
    </LinearLayout>


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    浏览器 显示flash问题
    类型参数的约束
    C# FUNC 应用
    c#抽奖系统
    3D基础数学小结
    google应用之字体引用
    MYSQL启动参数
    chrome中你不知道的快捷方式
    SQL Server 2008在添加用户时弹出15195错误
    Hibernate Maven Missing artifact javax.transaction:jta:jar:1.0.1B
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768447.html
Copyright © 2011-2022 走看看