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>


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

  • 相关阅读:
    Linux服务器上监控网络带宽命令
    boost编译很慢的解决方法
    python select poll
    python SocketServer
    boost implicit_cast
    函数名 函数名取地址 区别
    STL make_heap push_heap pop_heap sort_heap
    STL: fill,fill_n,generate,generate_n
    gcc __attribute__
    Linux命令 lsof使用
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768447.html
Copyright © 2011-2022 走看看