我们在项目中有一个带有"Key"元素的键盘,这个Key元素具有android:codes ="119",android:keyLabel ="w"等属性.
我的问题是如何包含一个自定义属性,如"android:alternativeKeyLabel"来做其他事情.
JPM*_*aes 54
此链接提供了一个肤浅的解释:http: //developer.android.com/guide/topics/ui/custom-components.html
考虑到你有一个继承自KeyboardView/View的CustomKeyboard:
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_keyboard"> <attr name="alternative_key_label" format="string" /> </declare-styleable> </resources>
在自定义组件中创建构造函数,覆盖接收属性集的默认构造函数,因为在加载布局时将调用此属性集.
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();
}
在布局文件中,添加自定义组件和资源链接.
Run Code Online (Sandbox Code Playgroud)<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>
出于任何其他目的,可以使用attrs构造函数参数检索在XML文件中声明自定义属性.
在我的情况下,我重用一个首选项自定义对话框,并设置这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<!-- something here -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <your.package.projectname.CustomView
        foo="bar"
    />
</PreferenceScreen>
然后在我的类构造函数中:
public CustomView(Context context, AttributeSet attrs) {
    String bar = attrs.getAttributeValue(null, "foo");
    Log.d("CustomView", "foo=" + bar);
}
android:keyLabel 是 Keyboard.Key 类为键盘上的每个键使用的众多 XML 属性之一。android:keyLabel 是您想要标记键的内容(如上面的“w”)。这些属性是为类预先定义的。“w”不是,但 android:keyLabel 是。如果您可以创建 android:alternativeKeyLabel 您希望该类用它做什么?我认为也许你应该尝试进一步解释你想要实现的目标。
| 归档时间: | 
 | 
| 查看次数: | 33578 次 | 
| 最近记录: |