2011.06.03 (2) ——— Android 1.6 Launcher research Customization ViewGroup
2011.06.03 (2) ——— Android 1.6 Launcher research Customization ViewGroup
1. Use XML as the view inside the ViewGroup
Reference: http://www.eoeandroid.com/thread-30888-1-1.html
Myviewgroup.java
Package Com.lp;import Android.content.context;import Android.util.attributeset;import android.util.Log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class MyViewGroup extends ViewGroup {public Myviewgroup (context context, AttributeSet attrs, int defstyle) {Super (context, ATTRS, Def Style); } public Myviewgroup (context context, AttributeSet Attrs) {Super (context, attrs); } public Myviewgroup (context context) {super (context); Initializeview (context); } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {System.out.println (Getchil DCount ()); for (int i=0;i<getchildcount (); i++) {View view = Getchildat (i); View.setvisibility (view.visible); View.measure (R-l, b-t); View.layout (L, T, R, b); }} private void Initializeview (context context) {//layoutinflater InflateR = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); Layoutinflater Inflater = layoutinflater.from (context); View mycontrol = inflater.inflate (R.layout.myview, NULL); AddView (mycontrol); }}
Myviewgroup.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" 800px "android:layout_height=" 600px "android:background=" #F33603 "Android:o rientation= "vertical" ><button android:text= "My button1" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content"/><button android:text= "My Button2" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content"/><button android:text= "My Button3" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content"/></linearlayout>
Mainactivity.java
Package Com.lp;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity { / * * Called when the activity is first created. * /@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (New Myviewgroup (This));} }
For the error in that URL just add view.measure (r-l, b-t);
2, some of their own sub-view
Reference: http://soft-app.iteye.com/blog/924757
Myviewgroup2.java
Package Com.lp;import Android.content.context;import Android.util.attributeset;import android.view.View;import Android.view.viewgroup;import Android.widget.button;public class MyViewGroup2 extends ViewGroup {public Myviewgrou P2 (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); } public MyViewGroup2 (context context, AttributeSet Attrs) {Super (context, attrs); } public MyViewGroup2 (context context) {super (context); Initializeview (context); } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {System.out.println (Getchil DCount ()); for (int i=0;i<getchildcount (); i++) {View view = Getchildat (i); View.setvisibility (view.visible); View.measure (R-l, b-t); View.layout (10+i*5, 20+i*10, View.getmeasuredwidth (), view. Getmeasuredheight ()); }} private void InitializeviEW (Context context) {button btn1 = new Button (context); Btn1.settext ("button 1"); AddView (BTN1); Button btn2 = New button (context); Btn2.settext ("button 2"); AddView (BTN2); }}
Mainactivity.java
Package Com.lp;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity { @ Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (New MyViewGroup2 (This));} }
Attention:
1, the Initializeview () method of the two is different from the former is to get the view from XML, the latter is to write their own view
2. Note the OnLayout () method
XML when
View.layout (L, T, R, b);
When writing yourself:
View.layout (10+i*50, 20+i*50, View.getmeasuredwidth (), view
. Getmeasuredheight ());
This means that the XML layout is already in the XML
Another: The use of custom ViewGroup
1, such as the above
Mainactivity.java
Package Com.lp;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity { @ Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (New MyViewGroup2 (This));} }
2, layout of the way
<?xml version= "1.0" encoding= "Utf-8"? ><com.lp.myviewgroup2 xmlns:android= "http://schemas.android.com/apk /res/android "android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <Button android:text= "AAA" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" /> <button android:text= "BBB" android:layout_width= "Wrap_content" android: layout_height= "60dip" /></com.lp.myviewgroup2>
Then Mainactivity.java
Package Com.lp;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity { @ Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.main); }}
Android 1.6 Launcher Research Custom ViewGroup (ext. 2011.06.03 (ii) ——— Android 1.6 Launcher research customization ViewGroup)