This is a problem when doing a skin-changing function.
For skin change, there are examples on the web that can be read from other skin packages, provided that you must first hold the reference name of the resource, like R.drawable.background (hey, this is not nonsense). The principle of this skin-changing scheme is that the resource name of the application is R.drawable.background, the skin package should also be the name, and then through this name to get the resource in the skin package of the specific ID, code:
Gets the local resource reference name first, the type name is "drawable" in R.drawable.background, and entry name is "background"
String restypename = GetContext (). Getresources (). Getresourcetypename (ID);
String resentryname = GetContext (). Getresources (). Getresourceentryname (ID);
Then create the context apk = GetContext () for the application of the skin package
. Createpackagecontext (PackageName,
Context.context_ignore _security)
//Then you get the resource ID in the skin package.
int drawavleid = Apk.getresources (). Getidentifier (Resentryname, Restypename,
apk.getpackagename ());
In this skin-changing solution, each activity needs to traverse the entire layout when switching the skin to determine if the control contains "skin" characters, meaning that the control is a control that needs to be changed, and the ID of the control can be saved first.
Code to traverse the view
Private list<integer> skinviewlist = new arraylist<integer> (); private void Scanviewgroup (ViewGroup group, list<integer> skinviewlist, resources Res) {//first We need check I F This viewgroup have a background if (Group.getid ()!= view.no_id && res.getresourceentryname (group.ge
TId ()). Contains (Skin_pattern) &&!skinviewlist.contains (group)) {Skinviewlist.add (Group.getid ());
//second check its child view view child;
for (int i = 0; i < Group.getchildcount (); i++) {child = Group.getchildat (i);
if (child instanceof ViewGroup) {scanviewgroup (viewgroup) Child, Skinviewlist, RES);
else if (child.getid () = = view.no_id) {return;
else {int viewid = Child.getid ();
String entryname = Res.getresourceentryname (Viewid);
Log ("Scanviewgroup (), entryname of this childview:" + entryname); if (Entryname.contains (Skin_pattern) &&!skinviewlist.contains (child)) Skinviewlist.add (Child.getid ());
}
}
}
The problem is, in the local application, you hold a control, such as a button, whose ID can be invoked directly by the Button.getid () method, but its background image background, we can call Button.getbackground () Method gets its object, but there is no way to get a reference name for the resource picture, and you cannot get its specific ID. The idea behind this is that every time the activity is initialized, we iterate over the property set AttributeSet of each control, the control that needs to be changed, and the value of the property that it android:background to save. Oncreateview (String name, context, AttributeSet Attrs) method that requires overloaded activity. My understanding of this approach is that each control in the activity (including LinearLayout, TextView, button, and so on) is invoked before initialization, and I also log and verify that the Attrs parameter is the property set of the control, which is what we need, code:
//First determine if the skinviewlist of the front scan is empty and not NULL means that there are controls that need to be replaced if (skinviewlist!= null && skin
Viewlist.size () > 0) {int viewid =-1, Backgroundid =-1; for (int i = 0; i < Attrs.getattributecount (); i++) {if (Attrs.getattributename (i). Equals ("id")) {Vie
WId = Attrs.getattributeresourcevalue (i,-1); } if (Attrs.getattributename (i). Equals ("background")) {Backgroundid = Attrs.getattributeresourcevalue (i
,-1);
}//check If background drawable need save if (Viewid!=-1 && backgroundid!=-1 && Drawableidlist!= null &&!drawableidlist.containskey (VIEWID)) {drawableidlist.put (Viewid, backg
RoundID);
Log ("Add to drawableidlist, Viewid =" + Viewid + ", Backgroundid =" + Backgroundid); }
}
With this backgroundid, we can get the reference name of the resource R.drawable.background, and then we can get the corresponding resource file from the other package by name, so we can perform the skin-changing operation. Moreover, this method not only can get the ID of the picture resource, but also can get the string such as r.string.title, font color such as r.color.red, font size such as r.dimens.text_size_small and so on, so as to enlarge the skin changing range.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.