标签:android style blog http color io os 使用 java
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup
1、用xml来作为ViewGroup里面的View
参考: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, defStyle); } 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(getChildCount()); 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:orientation="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)); } }
针对那个网址里面的错误 只要加上view.measure(r - l, b - t);就可以里了
2、自己些子view
参考: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 MyViewGroup2(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(getChildCount()); 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("按钮1"); addView(btn1); Button btn2 = new Button(context); btn2.setText("按钮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)); } }
注意:
1、两者的initializeView()方法不同 前者是从xml中获取view;后者是自己编写view
2、注意onLayout()方法
xml时
view.layout(l, t, r, b);
自己写时:
view.layout(10+i*50, 20+i*50, view.getMeasuredWidth(), view
.getMeasuredHeight());
也就是说 xml时 布局已经在xml里面说明了
另: 自定义ViewGroup的使用
1、如上面的
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的方式
<?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>
然后 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研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )
标签:android style blog http color io os 使用 java
原文地址:http://www.cnblogs.com/qingchen1984/p/3993508.html