添加一个按钮到Android的一个电话的onDraw [英] Add a button to an onDraw call in Android

查看:134
本文介绍了添加一个按钮到Android的一个电话的onDraw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是code下面绘制简单的2D图形(这是我在DroidNova发现 - 非常有用!),我想补充我在一个XML文件(字符串名称定义了一个按钮,位置)。我看不到如何将按钮添加到屏幕(同时仍显示来自呼叫的onDraw图形...

更新

我根据答案更新下面的code。我可以得到矩形绘制的,但它并不显示的按钮。

 公共类MainActivity延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        Drawresult();
    }    保护无效Drawresult(){
        //创建一个新的线性布局,显示了自定义图形和一个按钮。
        的LinearLayout mainLayout =新的LinearLayout(本);
        mainLayout.setOrientation(LinearLayout.VERTICAL);        //添加我们的自定义面板。
        mainLayout.addView(新面板(本));        //创建和设置我们的按钮。
        按钮myButton的=新按钮(本);
        myButton.setText(点击我);
        myButton.setOnClickListener(新OnClickListener(){             @覆盖
             公共无效的onClick(视图v){
                 完();
             }
         });         //我们的按钮添加到布局。
         mainLayout.addView(myButton的);         //设置这个活动的内容,我们的布局。
         的setContentView(mainLayout);
     }     类面板扩展视图{
         公共面板(上下文的背景下){
             超级(上下文);
         }         @覆盖
         公共无效的onDraw(帆布油画){             矩形R =新的矩形();
             r.set(60,60,260,77);             涂料粉刷=新的油漆();
             paint.setColor(Color.WHITE);
             paint.setStyle(Paint.Style.STROKE);
             paint.setStrokeWidth(3);             canvas.drawRect(R,漆);
         }
     }
}


解决方案

更改您的构造函数(假设你的子类的活动,否则发布更多的code的):

 保护无效Drawresult(){
    //创建一个新的线性布局,显示了自定义图形和一个按钮
    的LinearLayout mainLayout =新的LinearLayout(本);
    mainLayout.setOrientation(LinearLayout.VERTICAL);    //添加我们的自定义面板
    mainLayout.addView(新面板(本));    //创建和设置我们的按钮
    按钮myButton的=新按钮(本);
    myButton.setText(点击我);
    myButton.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            //把你的点击这里的逻辑
        }
    });    //添加我们的按钮布局
    mainLayout.addView(myButton的);    //设置这个活动的内容,我们的布局
    的setContentView(mainLayout);
}

I'm using the code below for drawing simple 2D graphics (which I found on DroidNova - very useful!), and I would like to add a button which I have defined in an XML file (string name and location). I can't see how to add the button to the screen (while still showing the graphics from the onDraw call...

Update

I've updated the code below based on the answer. I can get the rectangle to draw, but it does not show the button.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Drawresult();
    }

    protected void Drawresult() {
        // Create a new linear layout to display out custom graphics and a button.
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        // Add our custom panel.
        mainLayout.addView(new Panel(this));

        // Create and setup our button.
        Button myButton = new Button(this);
        myButton.setText("Tap Me");
        myButton.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {
                 finish();
             }
         });

         // Add our button to the layout.
         mainLayout.addView(myButton);

         // Set this activity's content to our layout.
         setContentView(mainLayout);
     }

     class Panel extends View {
         public Panel(Context context) {
             super(context);
         }

         @Override
         public void onDraw(Canvas canvas) {

             Rect r = new Rect();
             r.set(60, 60, 260, 77);

             Paint paint = new Paint();
             paint.setColor(Color.WHITE);
             paint.setStyle(Paint.Style.STROKE);
             paint.setStrokeWidth(3);

             canvas.drawRect(r, paint);
         }
     }
}

解决方案

Change your constructor to this (Assuming you have subclassed Activity. Otherwise post more of your code):

protected void Drawresult() {
    // Create a new linear layout to display out custom graphics and a button
    LinearLayout mainLayout = new LinearLayout(this);
    mainLayout.setOrientation(LinearLayout.VERTICAL);

    // Add our custom panel
    mainLayout.addView(new Panel(this));

    // Create and setup our button 
    Button myButton = new Button(this);
    myButton.setText("Tap Me");
    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Put your click logic here
        }
    });

    // Add our button to the layout
    mainLayout.addView(myButton);

    // Set this activity's content to our layout
    setContentView(mainLayout);
}

这篇关于添加一个按钮到Android的一个电话的onDraw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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