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>


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

  • 相关阅读:
    docker学习-docker核心技术
    Ecmall 中国地区设置
    lazyload.js实现图片异步载入
    inpyt 按钮变透明 边框
    vertical-align0 垂直对齐- 图片 兼容个浏览器
    div文字超出
    jquery满屏滚动代码
    $().index() 两种用法
    setTimeout不断重复执行
    background属性
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768447.html
Copyright © 2011-2022 走看看