无法设置内容视图(需要一个用于球,一个用于布局)

时间:2014-07-08 00:39:08

标签: android layout sensor motion

你好:)我的应用程序有问题,我需要使用两次setContentView,这使我的应用程序只选择一个,所以这是我的onCreate:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    **setContentView(R.layout.activity_main);** (setting the layout)
    highscore_int = (TextView) findViewById(R.id.highscore_int);
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey",
            Context.MODE_PRIVATE);
    time = prefs.getInt("key", 0);
    highscore_int.setText("Highscore:" + time + "seconds.");
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    lastUpdate = System.currentTimeMillis();
    animatedView = new AnimatedView(this);
    **setContentView(animatedView);** (setting the ball .. 
    }

对于那些问我为什么设置球的人,我的应用程序是基于运动传感器的球,这里是onCreate之后的所有内容:

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            sensorY = (int) event.values[1];
            sensorX = (int) event.values[0];
            x -= sensorX * 3;
            y += sensorY * 3;
            if (x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) {
                prefs = this.getSharedPreferences("myPrefsKey",
                        Context.MODE_PRIVATE);
                int oldScore = prefs.getInt("key", 0);
                if (TimeCounter > oldScore) {
                    Editor edit = prefs.edit();
                    edit.putInt("key", TimeCounter);
                    edit.commit();
                }
                finish();
                Intent myIntent = new Intent(this, YouLost.class);
                startActivity(myIntent);
            }
        }
    }

    public class AnimatedView extends ImageView {

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        static final int width = 50;
        static final int height = 50;

        @SuppressLint("NewApi")
        public AnimatedView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            display.getSize(size);
            screen_width = size.x;
            screen_height = size.y;
            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xffffAC23);
            mDrawable.setBounds(0, 0, screen_width, screen_height);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            mDrawable.setBounds(x, y, x + width, y + height);
            if (firstDraw) {
                x = screen_width / 2;
                y = screen_height / 2;
                firstDraw = false;
            }
            mDrawable.draw(canvas);
            invalidate();
        }
    }
}

那么我如何使用我的activity_main布局并仍然拥有移动球?

1 个答案:

答案 0 :(得分:0)

设置内容视图会替换已存在的所有视图。所以你不能两次调用它并保留两组。你需要重新设计一些东西,这样你就可以拥有1个布局。