是否有可能比在一类中的一个的setContentView吗? [英] Is it possible to have more than one setContentView in one class?

查看:104
本文介绍了是否有可能比在一类中的一个的setContentView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印的矩形形状和主要XML藏汉因为我想要的形状和数字,这可能吗?如果不是我可以打印从Java code的形状,我的主XML无论在我的模拟器?谢谢

 包com.example.accel;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.RectF;
进口android.graphics.drawable.ShapeDrawable;
进口android.graphics.drawable.shapes.RectShape;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.TextView;公共类AccelActivity扩展活动实现SensorEventListener
{
    / **当第一次创建活动调用。 * /
    CustomDrawableView mCustomDrawableView = NULL;
    ShapeDrawable mDrawable =新ShapeDrawable();
    公共静态INT X;
    公共静态INTÿ;
    公共静态INT Z者除外;
    私人的SensorManager的SensorManager = NULL;
    TextView的X1,Y1,Z1;
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {        super.onCreate(savedInstanceState);
        //获取到的SensorManager的引用
        的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
        mCustomDrawableView =新CustomDrawableView(本);
        的setContentView(mCustomDrawableView);
        的setContentView(R.layout.main);    }    //此方法将更新新的传感器事件UI
    公共无效onSensorChanged(SensorEvent sensorEvent)
    {
        {
            如果(sensorEvent.sensor.getType()== Sensor.TYPE_ACCELEROMETER){                TextView的TVX =(的TextView)findViewById(R.id.x_axis);
                TextView的TVY =(的TextView)findViewById(R.id.y_axis);
                TextView的TVZ =(的TextView)findViewById(R.id.z_axis);                X =(int)的Math.pow(sensorEvent.values​​ [0],2);
                Y =(int)的Math.pow(sensorEvent.values​​ [1],2);
                Z =(int)的Math.pow(sensorEvent.values​​ [2],2);            }         //如果(sensorEvent.sensor.getType()== Sensor.TYPE_ORIENTATION){          //}
        }
    }    //我选择不实现此方法
    公共无效onAccuracyChanged(传感器为arg0,ARG1 INT)
    {
        // TODO自动生成方法存根    }    @覆盖
    保护无效onResume()
    {
        super.onResume();
        //注册这个类作为加速度传感器的监听器
        sensorManager.registerListener(这一点,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                SensorManager.SENSOR_DELAY_FASTEST);
        // ...和定向传感器    }    @覆盖
    保护无效的onStop()
    {
        //注销监听器
        sensorManager.unregisterListener(本);
        super.onStop();
    }    公共类CustomDrawableView扩展视图
    {
        静态最终诠释宽度= 150;
        静态最终诠释高= 250;        公共CustomDrawableView(上下文的背景下)
        {
            超级(上下文);            mDrawable =新ShapeDrawable(新RectShape());
            。mDrawable.getPaint()的setColor(0xff74AC23);
            mDrawable.setBounds(X,Y,X +宽度,Y +高度);        }        保护无效的onDraw(帆布油画)
        {            RectF RECT =新RectF(AccelActivity.x,AccelActivity.y,AccelActivity.x +宽度,AccelActivity.y
                    +高); //设置矩形的界限
            涂料P =新的油漆(); //设置一些选项油漆
            p.setColor(Color.BLUE);
            canvas.drawRect(矩形,p)的;
            无效();
        }
    }
}


解决方案

是的,可以。

的setContentView(anotherLayout),所以你普林在活动中新的布局。

或者你可以在你mainLayout定义其它元素,你通过函数setVisibily操纵它

如果您需要elementA:

  elementA = findViewById(R.id.elementA);
elementA.setVisibility(真);

,如果你不需要它:

  elementA.setVisibility(假);

I am trying to print the rectangle shape and the main xml aswell as I want the shape and the figures, is this possible? If not how can I print a shape from java code and my main xml both on to my emulator? Thank you

package com.example.accel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AccelActivity extends Activity implements SensorEventListener
{
    /** Called when the activity is first created. */
    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public static int x;
    public static int y;
    public static int z;
    private SensorManager sensorManager = null;
    TextView x1, y1, z1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        // Get a reference to a SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);
        setContentView(R.layout.main);

    }

    // This method will update the UI on new sensor events
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

                TextView tvX= (TextView)findViewById(R.id.x_axis);
                TextView tvY= (TextView)findViewById(R.id.y_axis);
                TextView tvZ= (TextView)findViewById(R.id.z_axis);

                x = (int) Math.pow(sensorEvent.values[0], 2); 
                y = (int) Math.pow(sensorEvent.values[1], 2);
                z = (int) Math.pow(sensorEvent.values[2], 2);

            }

         //   if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

          //  }
        }
    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // Register this class as a listener for the accelerometer sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
        // ...and the orientation sensor

    }

    @Override
    protected void onStop()
    {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View
    {
        static final int width = 150;
        static final int height = 250;

        public CustomDrawableView(Context context)
        {
            super(context);

            mDrawable = new ShapeDrawable(new RectShape());
            mDrawable.getPaint().setColor(0xff74AC23);
            mDrawable.setBounds(x, y, x + width, y + height);

        }

        protected void onDraw(Canvas canvas)
        {

            RectF rect = new RectF(AccelActivity.x, AccelActivity.y, AccelActivity.x + width, AccelActivity.y
                    + height); // set bounds of rectangle
            Paint p = new Paint(); // set some paint options
            p.setColor(Color.BLUE);
            canvas.drawRect(rect, p);
            invalidate(); 
        }
    }
}

解决方案

yes, you can.

setcontentview(anotherLayout), so you pring the new layout in your activity.

or you can define other elements in your mainLayout and you manipulate it by the function setVisibily

if you need elementA:

elementA = findViewById(R.id.elementA);
elementA.setVisibility(true);

and if you don't need it:

elementA.setVisibility(false);

这篇关于是否有可能比在一类中的一个的setContentView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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