一个面板上绘制 [英] drawing on a panel

查看:114
本文介绍了一个面板上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*的我想提请称为maview$ P $后pssing按钮布顿面板上的线。但我的计划就是借鉴利用的总体布局线,以及按钮消失。 你有一个想法?谢谢! * 的 我的code:

 包esslineter.pack;
进口android.app.Activity;
进口android.os.Bundle;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.view.View;
进口android.widget.TextView;
进口android.content.Context;

公共类EsslineterActivity延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }
    公共无效布顿(查看视图)
    {
        maView CV =新maView(本);
        的setContentView(CV); cv.invalidate();
    }
    公共类maView扩展视图
    {
            公共maView(上下文的背景下)
            {
              超(上下文);
            }
            @覆盖
            保护无效的OnDraw(帆布油画)
        {
             涂料P =新的油漆();
             p.setColor(Color.WHITE);
             p.setStyle(Paint.Style.STROKE);
             p.setStrokeWidth(3);
             canvas.drawColor(Color.BLUE);
             canvas.drawLine(0,0,100,100,p)的;
        }
    }
}
 

我的布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/你好/>
    <按钮
        机器人:ID =@ + ID / bouton1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:的onClick =布顿
        机器人:文本=行画/>
    < View类=EsslineterActivity.maView
        机器人:ID =@ + ID / surfaceView1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0.86/>
< / LinearLayout中>
 

解决方案

当你调用的setContentView(CV),你正在改变整个活动的布局,一切都消失了, maView 成为屏幕上显示的只读视图。

相反,你可以隐藏你认为在你的XML布局(安卓知名度=水涨船高),并显示它被点击的按钮时

 < View类=EsslineterActivity.maView
    机器人:ID =@ + ID /厂景
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:能见度=水涨船高/>
 

而在你的code:

 公共无效布顿(查看视图)
{
    查看maView = findViewById(R.id.view1);
    maView.setVisibility(View.VISIBLE);
}
 

在一般的什么是错的,如果你要调用的setContentView()多次在同一个活动。它应该被称为只有一次,在的onCreate()方法的开头。

*I would like to draw a line on the panel called "maview" after pressing a button "bouton". But my program draws the line on the overall layout, and the button disappears. Have you an idea? Thank you! * my code:

package esslineter.pack;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.TextView;
import android.content.Context;

public class EsslineterActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.main);  
    }
    public void bouton (View view)
    {   
        maView cv=new maView(this);
        setContentView(cv); cv.invalidate(); 
    }   
    public  class  maView extends View
    {              
            public  maView(Context context)
            {
              super(context);                   
            }
            @Override
            protected void onDraw(Canvas canvas)
        {
             Paint p = new Paint();
             p.setColor(Color.WHITE);
             p.setStyle(Paint.Style.STROKE); 
             p.setStrokeWidth(3); 
             canvas.drawColor(Color.BLUE);                    
             canvas.drawLine(0,0, 100, 100, p);                                           
        }                   
    }
}

my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/bouton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="bouton"
        android:text="draw line" />
    <View class="EsslineterActivity.maView"
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.86" />
</LinearLayout>

解决方案

When you call setContentView(cv), you are changing the entire activity layout so everything disappears and maView becomes the only view displayed on the screen.

Instead you can hide your view in your XML layout (android:visibility="gone"), and show it when your button is clicked.

<View class="EsslineterActivity.maView"
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

And in your code :

public void bouton (View view)
{   
    View maView = findViewById(R.id.view1);
    maView.setVisibility(View.VISIBLE);
}   

In general something is wrong if you have to call setContentView() multiple times in the same activity. It should be called only once, at the beginning of the onCreate() method.

这篇关于一个面板上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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