Properties configuration for Android custom controls

Source: Internet
Author: User

In the Android XML layout file, xmlns:android= "Http://schemas.android.com/apk/res/android", which defines the XML namespace, starts with Android, For example: android:id= "@+id/i_am"

Sometimes we also want to customize the namespace. For example, under Xmlns:android= "Http://schemas.android.com/apk/res/android", write:xmlns:app= "http://schemas.android.com/ Apk/res-auto ". you can then set properties under the control in the entire XML layout file, for example:app:me= "@drawable/is_me". However, this attribute must be declared in Res/values/attrs.xml:

1 <  name= "isme">2         <"name=" Me "  format=" Reference "/>3</  Declare-styleable>

The properties are set, but how do you get the properties of these controls in the Java code?

1  Publiccustomcomponent (Context context, AttributeSet attrs) {2     Super(context, attrs);3 4TypedArray A =context.obtainstyledattributes (attrs,r.styleable.cuscomponent);5 6     intImagesrcid;7 8     Try {9Imagesrcid =A.getresourceid (r.styleable.isme_me,r.drawable.myimage);Ten  One}finally { A a.recycle (); -     } -Layoutinflater Inflater =Layoutinflater.from (context); the  -Inflater.inflate (R.layout.custom_component_layout, This,true);//to set a layout for a custom control -b = (ImageButton) Findviewbyid (R.ID.BTN);//get to the ImageButton on the layout - B.setimageresource (imagesrcid); +  -}

Another point is that the problem with namespaces is the difference between Res and Res-auto . Usually when we use custom attributes in a layout file, this is written xmlns:app=http://schemas.android.com/apk/res-auto, but if you are currently working on the project as LIB, then you write as above, There are errors that cannot be found for the custom property. At this point you have to write the xmlns:app=http://schemas.android.com/apk/res/package name path.

If the custom attribute has an error, such as:unexpected namespace prefix "app" found for tag fragment. Workaround:

To the eclipse's Problems tab to find this error, right-click and select the quick fix menu, and choose not to check the file in the pop-up menu (Disable Check in this files only).

Before I read so many other articles, but I am very lazy, did not write the article. Therefore, this time encountered the configuration of the custom attribute problem, so the problem I encountered and the solution to write down, for reference. Write for the first time, do not spray.

Properties configuration for Android custom controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.