Add custom controls for Android Widgets

Source: Internet
Author: User

First, let's look at a reference:

Aremoteviews object (and, consequently, an app widget) can support thefollowing layout classes:

* Framelayout

* Linearlayout

* Relativelayout

 

Andthe following widget classes:

 

* Analogclock

* Button

* Chronometer

* Imagebutton

* Imageview

* Progressbar

* Textview

Descendantsof these classes are not supported.

 

We can see Widget Only the following controls can be used: Analogclock ,Button ,Chronometer ,Imagebutton ,Mageview ,Progressbar ,Textview This 7 Type, Listview , Editview , Scrollview These commonly used controls cannot be found in our Widget . In fact, the source code of all the controls is Framework/base/CORE/Java/Android/widget In this directory 7 Controls are available because @ Remoteview Here, we can look at the source code:

 
Analogclock. Java: 39: @ remoteview

Analogclock. java-40-publicclass analogclockExtendsView {


Imagebutton. Java: 66: @ remoteview

Imagebutton. java-67-publicclass imagebuttonExtendsImageview {

.....

 

So we want Widget Such Listview For such controls , You need to write Listview Identical class , Add @ Remoteview Tag , And copy Framework/base/CORE/Java/Android/widget Under this directory.

Then we canWidgetUse the control we wrote in, because it has the same@ RemoteviewTag,Then he can beRemoteviewThe object is recognized.

 

Now that we know the principle, you can follow the steps below to implementAnalogclockFor example ):

 

 

 

1.First, I copiedAnalogclock. JavaName itMyclock. JavaToFramework/base/CORE/Java/Android/widgetUnder this directory, and then modified as neededCode.

 

 

2.ThisMyclock. JavaResource files used must be stored inFrameworks/base/CORE/RES/ResDirectory. It must be referenced in this way:Com. Android. Internal. R. drawable .*

However, changing resources is not convenient. We know that each system control hasStyleFile, so my approach is:

 

 

First look at the system'sAnalogclock. JavaOfStyleSource file:

Frameworks/base/CORE/RES/values/attrs. xml:

<Declare-styleableName= "Analogclock">

<!-Save some code->

<Attrname= "Hand_minute"Format= "Reference"/>

</Declare-styleable>

 

 Private Drawable mminutehand;

Public Myclock (context, attributeset attrs, intdefstyle ){

Super (Context, attrs, defstyle );

Resources r = context. getresources ();


Typedarray A = context. obtainstyledattributes (attrs, Com. Android. Internal. R. styleable. analogclock, defstyle, 0 );

Mminutehand = A. getdrawable (COM. Android. Internal. R. styleable. analogclock_hand_minute ); // The parameters in attrs. XML are called.


If (Mminutehand =Null ){

Mminutehand = R. getdrawable (COM. Android. Internal. R. drawable. clock_hand_minute ); // In this example, resources under the frameworks/base/CORE/RES directory are called.

}


}

On my ownWidgetIs defined in the layout configuration file:

<Myclock

Xmlns: Android= "Http://schemas.android.com/apk/res/android"

Android: ID= "@ + ID/rl_widget_clockview"

<! -Save some code->

Android: hand_minute = "@ drawable/minute_white" // reference resources under local drawable

Android: layout_width = "80dp"

Android: layout_height = "80dp"

/>

.. I also tested the source code repeatedly.

 

 

3. at this time, most of the work has been completed, we need to compile the entire SDK . However, note that I have encountered various compilation errors during compilation, and the prompts are unknown , this is very confusing. I finally come up with a very high success rate compilation method.

first in the root directory make-j4 compile the entire pure source code (do not add any classes and resources defined by ourselves), about 1-2 after compilation, copy the custom classes and resources to framework bottom, again in the root directory make-j4 compile.

To test the added code, runEmulatorCommand to start a new simulator, and then install ourAPK.

Basically, I have already reached the conclusion. I may have missed some trivial issues. I can discuss them with you.

Related Article

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.