我怎样才能在XML为Android创建我的自定义属性? [英] How can I create my custom properties on xml for Android?

查看:199
本文介绍了我怎样才能在XML为Android创建我的自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们的项目与钥匙的元素,这主要内容有属性如Android键盘:codeS =119,机器人:keyLabel =W等

我的问题是我怎么能包括自定义属性就好了。机器人:alternativeKeyLabel做别的事情

解决方案

这个链接给出了一个肤浅的解释: <一href="http://developer.android.com/guide/topics/ui/custom-components.html">http://developer.android.com/guide/topics/ui/custom-components.html

考虑你有一个CustomKeyboard继承自KeyboardView /查看:

    在水库/价值/ attrs.xml文件(创建文件,如果它不存在)
  1. 创建自定义属性:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
   &LT;申报,设置样式名称=custom_keyboard&GT;
        &LT; attr指示NAME =alternative_key_label格式=字符串/&GT;
    &LT; /申报,设置样式&GT;

&LT; /资源&GT;
 

  1. 在创建自定义组件构造覆盖接收设置属性默认构造函数,因为当布局加载此人会被调用。

     公共CustomKeyboard(上下文的背景下,AttributeSet中集){
        超(背景下,设置);
        TypedArray A = context.obtainStyledAttributes(套,R.styleable.custom_keyboard);
        CharSequence中= a.getString(R.styleable.custom_keyboard_alternative_key_label);
        如果(S!= NULL){
            this.setAlternativeKeyLabel(s.toString());
        }
        a.recycle();
    }
     

  2. 在你的布局文件,你的自定义组件和链接添加到您的资源。

 &LT;布局的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res/your.package.ProjectName
    ... /&GT;
    ...
    &LT; your.package.projectname.CustomKeyboard
        机器人:ID =@ + ID / my_keyboard
        ...
        应用程序:alternative_key_label =F&GT;
    &LT; /your.package.projectname.CustomKeyboard>
&LT; /布局和GT;
 

We have in our project a keyboard with "Key" elements, this Key elements have attributes such as android:codes="119", android:keyLabel="w" and so on.

My question is how can I include an custom attribute like a "android:alternativeKeyLabel" to do something else.

解决方案

This link gives a superficial explanation: http://developer.android.com/guide/topics/ui/custom-components.html

Considering you have a CustomKeyboard that inherits from KeyboardView/View:

  1. Create your custom properties in res/values/attrs.xml file (create the file if it does not exist):

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="custom_keyboard">
        <attr name="alternative_key_label" format="string" />
    </declare-styleable>

</resources>

  1. Create a constructor in your custom component overriding default constructor that receives the attribute set because this one will be called when the layout is loaded.

    public CustomKeyboard(Context context, AttributeSet set) {
        super(context, set);
        TypedArray a = context.obtainStyledAttributes(set,R.styleable.custom_keyboard);
        CharSequence s = a.getString(R.styleable.custom_keyboard_alternative_key_label);
        if (s != null) {
            this.setAlternativeKeyLabel(s.toString());
        }
        a.recycle();
    }
    

  2. In your layout file, add your custom component and the link to your resources.

 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/your.package.ProjectName"
    .../>
    ...
    <your.package.projectname.CustomKeyboard
        android:id="@+id/my_keyboard"
        ...
        app:alternative_key_label="F">
    </your.package.projectname.CustomKeyboard>
</Layout>

这篇关于我怎样才能在XML为Android创建我的自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆