一般继承自Layout,用LayoutInflater.from(context).inflate(R.layout.myview, this, true);初始内容。
下面构造函数是必须的,想在xml中用定义的控件就要有。xml布局文件中设置的属性对应AttributeSet attrs参数。
public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub c = context; LayoutInflater.from(context).inflate(R.layout.myview, this, true); }
下面的函数也是有必要的,想在代码中创建控件可能用到这个构造参数。
public MyView(Context context) { super(context); // TODO Auto-generated constructor stub c = context; LayoutInflater.from(context).inflate(R.layout.myview, this, true); }
示例
控件中有一个gallery和一个TextView,TextView自动显示gallery中当前view的一些信息。
myview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
MyView
package com.iteedu.myview; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery; import android.widget.LinearLayout; import android.widget.TextView; public class MyView extends LinearLayout { Gallery g; TextView info; Context c; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub c = context; init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub c = context; init(); } private void init() { LayoutInflater.from(c).inflate(R.layout.myview, this, true); g = (Gallery) this.findViewById(R.id.gallery); info = (TextView) this.findViewById(R.id.info); g.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub info.setText(parent.getItemAtPosition(position).toString()); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); g.setAdapter(new ImageAdapter(c)); } }
ImageAdapter
package com.iteedu.myview; import android.content.Context; import android.content.res.Resources; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class ImageAdapter extends BaseAdapter { private Context mContext; private Integer[] mImageIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_6 }; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return mImageIds[position]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageDrawable(mContext.getResources().getDrawable( mImageIds[position])); return imageView; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.iteedu.myview.MyView android:id="@+id/myView1" android:layout_width="fill_parent" android:layout_height="wrap_content" > </com.iteedu.myview.MyView> </LinearLayout>
MyViewActivity
package com.iteedu.myview; import android.app.Activity; import android.os.Bundle; public class MyViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }