如何在没有“setContentView(R.layout.main)"的情况下创建活动 [英] How to create an activity without 'setContentView(R.layout.main)'

查看:20
本文介绍了如何在没有“setContentView(R.layout.main)"的情况下创建活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过执行类似下面的代码来创建活动,其中视图不是从 xml 文件设置的,而是这样的:setContentView(new myView(this));

I know its possible to create activities by doing something like the code bellow, where the view is not set from xml file but like this: setContentView(new myView(this));

我不明白的是如何使用此代码但仍然可以自定义它,例如如果我想在下面的代码中添加一个按钮,我该怎么做,因为我不能简单地添加一个到 xml 布局可以吗?

What i don't understand is how to use this code but still have the ability to customize it, for instance if i wanted to add a button to the code bellow, how would i do it, because i cant simply add one to an xml layout can i?

对此的任何好的答案都将受到高度赞赏提前致谢!

ANY GOOD ANSWERS TO THIS WILL VERY MUCH APPRECIATED thanks in advance!

package com.faceapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

 public class FaceappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new myView(this));
    }

    private class myView extends View{

     private int imageWidth, imageHeight;
     private int numberOfFace = 5;
     private FaceDetector myFaceDetect; 
     private FaceDetector.Face[] myFace;
     float myEyesDistance;
     int numberOfFaceDetected;

     Bitmap myBitmap;


    public myView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
   myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

  }

  @Override
  protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub

            canvas.drawBitmap(myBitmap, 0, 0, null);

            Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE); 
            myPaint.setStrokeWidth(3);

            for(int i=0; i < numberOfFaceDetected; i++)
            {
             Face face = myFace[i];
             PointF myMidPoint = new PointF();
             face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();
             canvas.drawRect(
               (int)(myMidPoint.x - myEyesDistance),
               (int)(myMidPoint.y - myEyesDistance),
               (int)(myMidPoint.x + myEyesDistance),
               (int)(myMidPoint.y + myEyesDistance),
               myPaint);
            }
  }
    }
}

^^^^^^^^^^^^^^^^已回答

^^^^^^^^^^^^^^^ Answered

如何定位按钮和图像视图?(理想情况下使用相对布局)下图告诉你我的意思:(忽略图像重新调整大小)

How to position the button and imageview? (Ideally using relative layout) The picture bellow shows you what i mean: (Ignore that the image is re-sized)

新代码:

package com.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class TesttActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        Button button = new Button(this);
        button.setText("Button!");
        layout.addView(button);

        myView custom = new myView(this);
        layout.addView(custom);

        setContentView(layout);
    }

    private class myView extends View{

        private int imageWidth, imageHeight;
        private int numberOfFace = 5;
        private FaceDetector myFaceDetect; 
        private FaceDetector.Face[] myFace;
        float myEyesDistance;
        int numberOfFaceDetected;

        Bitmap myBitmap;


       public myView(Context context) {
      super(context);
      // TODO Auto-generated constructor stub

      BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
      BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
      myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
      imageWidth = myBitmap.getWidth();
      imageHeight = myBitmap.getHeight();
      myFace = new FaceDetector.Face[numberOfFace];
      myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
      numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

     }

     @Override
     protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub

               canvas.drawBitmap(myBitmap, 0, 0, null);

               Paint myPaint = new Paint();
               myPaint.setColor(Color.GREEN);
               myPaint.setStyle(Paint.Style.STROKE); 
               myPaint.setStrokeWidth(3);

               for(int i=0; i < numberOfFaceDetected; i++)
               {
                Face face = myFace[i];
                PointF myMidPoint = new PointF();
                face.getMidPoint(myMidPoint);
       myEyesDistance = face.eyesDistance();
                canvas.drawRect(
                  (int)(myMidPoint.x - myEyesDistance),
                  (int)(myMidPoint.y - myEyesDistance),
                  (int)(myMidPoint.x + myEyesDistance),
                  (int)(myMidPoint.y + myEyesDistance),
                  myPaint);
               }
     }
       }
   }

推荐答案

你可以通过 setContentView() 任何形式的视图,成为你布局的根视图.下面是一个动态构建的带有 Button 和 myView 的 LinearLayout.

You can pass setContentView() any form of view, to be the root view of your layout. Below is a dynamically built LinearLayout with a Button and your myView.

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        // Define the LinearLayout's characteristics
        layout.setGravity(Gravity.CENTER);
        layout.setOrientation(LinearLayout.VERTICAL);

        // Set generic layout parameters
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button button = new Button(this);
        button.setText("Button!");
        layout.addView(button, params); // Modify this

        myView custom = new myView(this);
        layout.addView(custom, params); // Of course, this too

        setContentView(layout);
    }
}

了解只有通过 setContentView() 一个 ViewGroup 才能将子视图添加到您的根视图中;比如RelativeLayout、LinearLayout等.换句话说,你不能这样做:

Understand that you can only add child views to your root view if you pass setContentView() a ViewGroup; like RelativeLayout, LinearLayout, etc. In other words you cannot do this:

        myView custom = new myView(this);

        Button button = new Button(this);
        button.setText("Button!");

        custom.addView(button); 
        // Nope! Method "addView()" does not exist for a regular View...

        setContentView(custom);

此外,命名约定建议类名中的每个单词的首字母大写.所以 myView 应该是 MyView,至少它让你的代码更容易被其他程序员阅读,编译器会用正确的颜色突出显示你的类变量.

Also, naming convention suggests that each word in a class name should have the first letter capitalized. So myView ought to be MyView, at a minimum it makes your code easier to read for other programmers and the compiler will highlight your class variables with the correct color.

这篇关于如何在没有“setContentView(R.layout.main)"的情况下创建活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆