Preface
The Android.view.View view class is the base class for widgets, with many extension classes, including text view TextView, image View ImageView, progress bar ProgressBar, view group ViewGroup, and so on.
The specific results are as follows:
Create Android Project
The Eclipse IDE is used for Android development.
The officially recommended IDE is already based on IntelliJ Idea's studio.
1. File-------android application Project
After entering the following, the next step:
2. Next to select Blank Activity
3. Next, Finish
Example Preview Introduction
Here's how the instance is used:
Define some buttons in mainactivity and click on the button to enter into the activity of the actual effect.
Modify Mainactivity
To modify the Activity_main.xml, the complete contents are as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=" com.oscar999.androidstudy.MainActivity "> <button Android:id= "@+id/widgetbutton" android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content " android:layout_gravity=" center " android:text=" @string/my_widget "/></relativelayout >
To modify the Strings.xml, the contents are as follows
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >androidstudy</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_settings" >Settings</string> <string name= "My_widget" >my widget</string> </resources>
Here, run a look at the effect:
add activity that clicks the button above
New Activity--mywidgetactivity
To modify Mainactivity.java, let the My Widget button enter this activity. The main is to modify the OnCreate method, the modified complete code is as follows:
Package Com.oscar999.androidstudy;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); final Button Widgetbutton = (Button) Findviewbyid (R.id.widgetbutton); Widgetbutton.setonclicklistener (New Onclicklistener () {public void OnClick (view view) {Intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Mywidgetactivity.class); startactivity (intent); Finish ();});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar Item CLicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
The next step is to improve the mywidgetactivity of the relevant content.Add three buttons
1. Modify Activity_my_widget.xml, place three button, complete the following:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:orientation=" vertical "> <!--Regular Sized button--><button android:id= "@+id/button_normal" android:text= "@string/button_normal" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content"/><!--Small button-->< Button android:id= "@+id/button_small" style= "? Android:attr/buttonstylesmall" android:text= "@string/ Button_small " android:layout_width=" wrap_content " android:layout_height=" wrap_content "/><!-- Toggle button--><togglebutton android:id= "@+id/button_toggle" android:text= "@string/button_toggle" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
2. Strings.xml Add the following configuration
<string name= "Button_normal" >Normal</string> <string name= "Button_small" >small</string > <string name= "Button_toggle" >On</string>
To run, the effect is as follows:
Add Image Area-ImageView
And the button above is added in a similar way: 1. In Activity_my_widget.xml, add the following:
<imageview android:src= "@drawable/image_sample1" android:contentdescription= "@string/image_sample1" Android:adjustviewbounds= "true" android:layout_width= "wrap_content" android:layout_height= "wrap_content"/ >
2. Find an image file, named Image_sample1.png, into the res/drawable-hdpi directory (other similar drawable directories can also be put)
3. Add the following in String.xml:
To run, the effect is as follows:
More controls
Other space like: Image button (ImageButton), progress bar (ProgressBar), editable text area (EditText), check box (checkbox), radio button Group (radiogroup), text area (TextView), Controls such as the Rotate button (Spinner). The specific definition can be done by reference to the API. In addition to the system-provided view, you can customize the view.
Custom View
1. New Class:com.oscar999.androidstudy.view.MyView inherit from view
Package Com.oscar999.androidstudy.view;import Com.oscar999.androidstudy.r;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.canvas;import Android.graphics.paint;import Android.util.attributeset;import Android.view.view;public class MyView extends View {private Paint mtextpaint;private int mascent;private String Mtext;public MyView (context context, AttributeSet Attrs) {Super (context, attrs); Initmyview () ; TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.myview); Charsequence s = a.getstring (R.styleable.myview_text), if (s! = null) {SetText (s.tostring ());} Retrieve the color (s) to being used for this view and apply them.//Note If you are only care about supporting a single color , that's you//can instead call A.getcolor () and pass the To SetTextColor (). SetTextColor (A.getcolor (R.styleable.myview_tex TColor, 0xff000000)); int textSize = A.getdimensionpixeloffset (r.styleable.myview_textsize, 0); if (TextSize > 0) { Settextsize (textSize);} A.rEcycle ();//TODO auto-generated constructor stub}private final void Initmyview () {//Create text paint to setting Textmtex Tpaint = new Paint (); Mtextpaint.setantialias (true);//must manually scale the desired text size to match screen Densitymte Xtpaint.settextsize (Getresources (). Getdisplaymetrics (). density); Mtextpaint.setcolor (0xff000000); SetPadding ( 3, 3, 3, 3);} public void SetText (String text) {MText = text;//call this when something have changed which has invalidated the layout// of this view. This would schedule a layout pass of the View Tree.requestlayout ();//Invalidate the whole view. If The view is visible,//OnDraw (Android.graphics.Canvas) would be a called at some point in the//future. This must is called from a UI thread. To call from a non-ui//thread, call Postinvalidate (). invalidate ();} /** * Sets the text size for this label * * @param size * Font size */public void settextsize (int size) {//Th is text size have been pre-scaled by the getdimensionpixeloffset//mEthodmtextpaint.settextsize (size); Requestlayout (); invalidate ();} /** * Sets the text color for this label. * * @param color * ARGB value for the text */public void settextcolor (int color) {mtextpaint.setcolor (color); I Nvalidate ();} /** * @see android.view.view#measure (int, int) */@Overrideprotected void onmeasure (int widthmeasurespec, int HEIGHTMEASURESPEC) {//This mehod must is called by onmeasure (int, int.) to store the//measured width and measured height. Failing to does so would trigger an//exception at Measurement time.setmeasureddimension (measurewidth (widthmeasurespec), mea Sureheight (Heightmeasurespec));} /** * Determines the width of this view * * @param measurespec * A Measurespec Packed to an int * @return Th E width of the view, honoring constraints from Measurespec */private int measurewidth (int measurespec) {int result = 0;int Specmode = Measurespec.getmode (measurespec); int specsize = Measurespec.getsize (Measurespec); if (SpecMode = = measurespec.exactly) {We were told how big to Beresult = specsize;} else {//Measure the Textresult = (int) mtextpaint.measuretext (mText) + getpaddingleft () + getpaddingright (); if (Specmode = = Measurespec.at_most) {//Respect At_most value if that's what are called for by//Measurespecresult = math.min (result, specsize);}} return result;} /** * Determines the height of this view * * @param measurespec * A Measurespec Packed to an int * @return T He height of the view, honoring constraints from Measurespec */private int measureheight (int measurespec) {int result = 0; int specmode = Measurespec.getmode (measurespec); int specsize = Measurespec.getsize (measurespec); mAscent = (int) Mtextpaint.ascent (); if (Specmode = = measurespec.exactly) {//We were told how big to Beresult = Specsize;} else {//Measur E The text (beware:ascent is a negative number) result = (int) (-mascent + mtextpaint.descent ()) + getpaddingtop () + Getpad Dingbottom (); if (Specmode = = Measurespec.at_most) {//Respect At_most value if tHat was the called for by//Measurespecresult = math.min (result, specsize);}} return result;} protected void OnDraw (canvas canvas) {super.ondraw (canvas); Canvas.drawtext (MText, This.getpaddingleft (), This.getpaddingtop ()-mascent, Mtextpaint);}}
2. Added Attrs.xml under res/layouts
<resources><declare-styleable name= "MyView" > <attr name= "text" format= "string"/ > <attr name= "textcolor" format= "color"/> <attr name= "textSize" format= "Dimension"/> ; </declare-styleable></resources>
3. Add Red.png, Blue.png, green.png to drawable directory
4. Modify Activity_my_widget.xml, complete the following:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http// Schemas.android.com/apk/res/com.oscar999.androidstudy "android:layout_width=" Wrap_content "android:layout_height = "Wrap_content" android:orientation= "vertical" > <!--Regular sized button--><button android:id= "@+id/b Utton_normal "android:text=" @string/button_normal "android:layout_width=" wrap_content "android:layout_height=" Wra P_content "/><!--Small button--><button android:id=" @+id/button_small "style="? android:attr/ Buttonstylesmall "android:text=" @string/button_small "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "/><!--Toggle button--><togglebutton android:id=" @+id/button_toggle "android:text=" @ String/button_toggle "android:layout_width=" wrap_content "android:layout_height=" wrap_content "/><!--Image View--><imageview android:src= "@drawable/image_sample1" Android:contentdescription= "@string/image_sample1" android:adjustviewbounds= "true" Android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content"/> <!--My view--> <com.oscar999.androidstudy.view.myview Android:bac kground= "@drawable/red" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" app:t ext= "Red"/> <com.oscar999.androidstudy.view.myview android:background= "@drawable/blue" Android Oid:layout_width= "Fill_parent" android:layout_height= "wrap_content" app:text= "Blue" app:textsize= "20SP" /> <com.oscar999.androidstudy.view.myview android:background= "@drawable/green" Android:layout_wi Dth= "Fill_parent" android:layout_height= "wrap_content" app:text= "Green" app:textcolor= "#ffffff"/> </LinearLayout>
5. Run it, the effect is as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[ANDROID5 Series II] 1. Full-instance controls (widgets)