How can I create my custom properties on xml for Android?

前端 未结 4 590
死守一世寂寞
死守一世寂寞 2020-12-04 17:59

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 questi

相关标签:
4条回答
  • 2020-12-04 18:24

    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":

    https://developer.android.com/training/custom-views/create-view#customattr

    0 讨论(0)
  • 2020-12-04 18:34

    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.

    0 讨论(0)
  • 2020-12-04 18:37

    For any other purpose, declaring a custom property in the XML file can be retrieve with attrs constructor parameter.

    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);
    }
    
    0 讨论(0)
  • 2020-12-04 18:42

    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>
    
    0 讨论(0)
提交回复
热议问题