Java 如何在 xml 上为 Android 创建自定义属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2029719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create my custom properties on xml for Android?
提问by Tebam
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.
我们在我们的项目中有一个带有“Key”元素的键盘,这个 Key 元素具有诸如 android:codes="119"、android:keyLabel="w" 等属性。
My question is how can I include an custom attribute like a "android:alternativeKeyLabel" to do something else.
我的问题是如何包含一个像“android:alternativeKeyLabel”这样的自定义属性来做其他事情。
回答by fupsduck
android:keyLabel is one of many XML attribute used by the Keyboard.Key class for each key on your keyboard. android:keyLabel is what you want to label the key with (like a "w" as in above). The attributes are pre-defined for the class. The "w" isn't but android:keyLabel is. If you could create android:alternativeKeyLabel what would you expect the class to do with it? I think maybe you should try to explain further what you are trying to accomplish.
android:keyLabel 是 Keyboard.Key 类为键盘上的每个键使用的众多 XML 属性之一。android:keyLabel 是你想用什么来标记键(如上面的“w”)。属性是为类预先定义的。“w”不是而是 android:keyLabel 。如果您可以创建 android:alternativeKeyLabel,您希望该类用它做什么?我想也许您应该尝试进一步解释您要完成的工作。
回答by Lance Nanek
You can create custom attributes for your own classes that extend View or a subclass. The process is documented in the "Creating a View Class" section of the Android Dev Guide under the heading "Define Custom Attributes":
您可以为自己的扩展 View 或子类的类创建自定义属性。该过程记录在 Android Dev Guide 的“Creating a View Class”部分的“Define Custom Attributes”标题下:
https://developer.android.com/training/custom-views/create-view#customattr
https://developer.android.com/training/custom-views/create-view#customattr
回答by JPMagalhaes
This link gives a superficial explanation: http://developer.android.com/guide/topics/ui/custom-components.html
这个链接给出了一个肤浅的解释:http: //developer.android.com/guide/topics/ui/custom-components.html
Considering you have a CustomKeyboard that inherits from KeyboardView/View:
考虑到您有一个继承自 KeyboardView/View 的 CustomKeyboard:
- Create your custom properties in res/values/attrs.xml file (create the file if it does not exist):
- 在 res/values/attrs.xml 文件中创建您的自定义属性(如果文件不存在,则创建该文件):
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_keyboard"> <attr name="alternative_key_label" format="string" /> </declare-styleable> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_keyboard"> <attr name="alternative_key_label" format="string" /> </declare-styleable> </resources>
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(); }
In your layout file, add your custom component and the link to your 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(); }
在您的布局文件中,添加您的自定义组件和资源链接。
<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>
<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>
回答by vono
For any other purpose, declaring a custom property in the XML file can be retrieve with attrs constructor parameter.
对于任何其他目的,可以使用 attrs 构造函数参数检索在 XML 文件中声明的自定义属性。
In my case I reuse a preference custom dialog, and set things like that:
就我而言,我重用了一个首选项自定义对话框,并设置了以下内容:
<?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>
Then in my class contructor:
然后在我的班级构造函数中:
public CustomView(Context context, AttributeSet attrs) {
String bar = attrs.getAttributeValue(null, "foo");
Log.d("CustomView", "foo=" + bar);
}