简体   繁体   English

如何合并两个视图?

[英]How to combine two Views?

I want to see on the screen both a button and a circle(drawing with canvas).But I can't do this , Especially in XML file seen good but when I send APK to my phone this error come : 我想在屏幕上同时看到一个按钮和一个圆圈(用画布绘制)。但是我无法做到这一点,特别是在XML文件中看的很好,但是当我将APK发送到手机时,会出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mehmet.catchtheball/com.example.mehmet.catchtheball.Customer}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.mehmet.catchtheball.MyView java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mehmet.catchtheball / com.example.mehmet.catchtheball.Customer}:android.view.InflateException:二进制XML文件行#0:二进制XML文件行0 :错误膨胀类com.example.mehmet.catchtheball.MyView

my activity_customer.xml showing screenshot : enter image description here 显示截图的我的activity_customer.xml在此处输入图像描述

(Shortly ,I want this showing on my phone too.) (很快,我也想在手机上显示此内容。)

MyView.java file:

package com.example.mehmet.catchtheball;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        paint.setColor(Color.parseColor("lightGray"));
        canvas.drawCircle(500, 500, 150, paint);
    }
}

Customer.java file : Customer.java文件:

package com.example.mehmet.catchtheball;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;


public class Customer extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer);
        MyView a = new MyView(this); //create a circle
        a.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
               return true;
            }
        });
    }
}

activity_customer.xml file : activity_customer.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mehmet.catchtheball.main">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="@string/button" />

    <RelativeLayout
        android:id="@+id/relativeLayoutt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true">

    </RelativeLayout>

    <com.example.mehmet.catchtheball.MyView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

You have to implement the following Constructors in your MyView class 您必须在MyView类中实现以下构造方法

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

Your class should look like as below:- 您的课程应如下所示:-

public class MyView extends View {

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyView(Context context) {
        super(context);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        paint.setColor(Color.parseColor("lightGray"));
        canvas.drawCircle(500, 500, 150, paint);
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM