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