小弟初学android,今天看到一个样例public class shiyan extends Activity 
       {
   
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new myview(this));         }public class myview extends View {
  Bitmap bmp;
  Bitmap bmp2;
 
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
Resources res=context.getResources();
bmp=BitmapFactory.decodeResource(res,R.drawable.blackzhu);
bmp2=BitmapFactory.decodeResource(res,R.drawable.bitmap1);

//Resources resources = getContext().getResources();  
//Drawable btnDrawable = resources.getDrawable(R.drawable.bitmap1); 
//Layout.setBackgroundDrawable(btnDrawable); 

}
  @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);

canvas.drawBitmap(bmp2, 0, 0,new Paint());
canvas.drawBitmap(bmp, 40, 130,new Paint());
}}程序中Acivity的setContentView](new myview(this),调用了myview,myview为一个新建的类在这个程序中myview与Acivity是什么关系,在myview中能不能定义控件,比如TextView,怎样定义
请哪位前辈指教一下,不胜感激!

解决方案 »

  1.   

    myview就是view类的,没有试过往里面加控件,应该是不行的。
    setContentView就是创建activity的时候要设置显示的界面布局,这里设为一个view,你也可以自己去制作layout,然后setContentView进去就是你要的布局了。如果你要textview,那可以在main.xml(这个是创建空工程自带的,你也可以自己定义)中,添加<TextView android:id="@+id:text">
    </TextView>这样在setContentView时就直接填id就可以了。
    setContentView(R.id.text);
      

  2.   

    这里myview是shiyan来的一个子类,用于设置父类的view。
    可以在myview中增加TextView控件,要先设置一个layout,把textview放在layout上,并且把layout设置为shiyan的view。
    public class AndTest extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new myview(this).layout); 
            
        }
     
        public class myview extends View {
           Bitmap bmp;
           Bitmap bmp2;
           TextView tv;
           LinearLayout layout;
          
         public myview(Context context) {
         super(context);
         // TODO Auto-generated constructor stub
         Resources res=context.getResources();
         bmp=BitmapFactory.decodeResource(res,R.drawable.icon);
         bmp2=BitmapFactory.decodeResource(res,R.drawable.icon);

         //Resources resources = getContext().getResources();   
         //Drawable btnDrawable = resources.getDrawable(R.drawable.bitmap1);  
         //Layout.setBackgroundDrawable(btnDrawable);  
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT);
         LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);      layout = new LinearLayout(AndTest.this);
         layout.setLayoutParams(lp);
         tv = new TextView(AndTest.this);
         tv.setText("Hello world!\nYou are welcome!");
         layout.addView(tv, lp1);
              }
         @Override
         protected void onDraw(Canvas canvas) {
         // TODO Auto-generated method stub
         super.onDraw(canvas);
         canvas.drawColor(Color.WHITE);

         canvas.drawBitmap(bmp2, 0, 0,new Paint());
         canvas.drawBitmap(bmp, 40, 130,new Paint());
        }    }
    }
      

  3.   

    这样会覆盖掉你原有的布局,如果想要自定义布局,最好不要自定义view