Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将画布中drawRect方法绘制的5个矩形添加到android中的layout.xml_Java_Android_Android Layout - Fatal编程技术网

Java 将画布中drawRect方法绘制的5个矩形添加到android中的layout.xml

Java 将画布中drawRect方法绘制的5个矩形添加到android中的layout.xml,java,android,android-layout,Java,Android,Android Layout,我在画布中使用drawRect方法在屏幕上创建了5个不同颜色的矩形。我想在屏幕底部添加一个搜索栏,当矩形从左向右移动时,它会逐渐改变矩形的颜色。我在向布局中添加矩形时遇到问题,程序崩溃 下面是我的矩形类,它绘制了5个矩形: public class RectAngle extends View { public RectAngle(Context context) { super(context); setFocusable(true); }

我在画布中使用drawRect方法在屏幕上创建了5个不同颜色的矩形。我想在屏幕底部添加一个搜索栏,当矩形从左向右移动时,它会逐渐改变矩形的颜色。我在向布局中添加矩形时遇到问题,程序崩溃

下面是我的矩形类,它绘制了5个矩形:

public class RectAngle extends View {
    public RectAngle(Context context) {
        super(context);
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float x = getWidth();
        float y = getHeight();

        Paint topLeftRect = new Paint();
        Paint bottomLeftRect = new Paint();
        Paint topRightRect = new Paint();
        Paint midRightRect = new Paint();
        Paint bottomRightRect = new Paint();

        // Draw  the top left rectangle
        topLeftRect.setStyle(Paint.Style.FILL);
        topLeftRect.setColor(Color.parseColor("#FFFF99"));
       // canvas.drawPaint(topLeftRect);
        canvas.drawRect(0, 0, x / 2, y / 2, topLeftRect);


        //Draw the bottom left rectangle

        bottomLeftRect.setStyle(Paint.Style.FILL);
        //bottomLeftRect.setColor(Color.WHITE);
       // canvas.drawPaint(bottomLeftRect);
        bottomLeftRect.setColor(Color.parseColor("#FFFFFF"));
        canvas.drawRect(0, y / 2, x / 2, y, bottomLeftRect);

        //Draw the top tight rectangle

        topRightRect.setStyle(Paint.Style.FILL);
        //topRightRect.setColor(Color.WHITE);
        topRightRect.setColor(Color.parseColor("#FF6600"));
        //canvas.drawPaint(topRightRect);
        canvas.drawRect(x / 2, 0, x, y / 3, topRightRect);

        // Draw the middle right rectangle

        midRightRect.setStyle(Paint.Style.FILL);
        //midRightRect.setColor(Color.WHITE);
        midRightRect.setColor(Color.parseColor("#66FFFF"));
       // canvas.drawPaint(midRightRect);
        canvas.drawRect(x / 2, y/3, x, 2*y / 3, midRightRect);

        //Draw the bottom right rectangle

        bottomRightRect.setStyle(Paint.Style.FILL);
        //bottomRightRect.setColor(Color.WHITE);
        bottomRightRect.setColor(Color.parseColor("#CCCC00"));
       // canvas.drawPaint(bottomRightRect);
        canvas.drawRect(x/2, 2*y/3, x, y, bottomRightRect);




    }
}
这是我的主要活动课:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new RectAngle(this));
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
我知道setContentView(new RectAngle(this))调用会在屏幕上绘制矩形,但它不会将其添加到搜索栏使用的xml文件中

以下是我的activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <com.example.hassan.modernartui.RectAngle
        android:id="@+id/rectangle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:max="100" />
</RelativeLayout>


谢谢你的帮助。

我认为问题在于你没有定义正确的构造函数

在第一个构造函数之后添加其他2个构造函数,如下所示

public RectAngle(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public RectAngle(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

谢谢毗瑟奴。你知道我怎样才能使矩形没有任何填充物,并填满整个屏幕吗?另外,我想将搜索栏移动到最底部(矩形的底部)。我在上面添加了我的模拟器的屏幕截图。再次感谢xml中的矩形视图,请在上面添加布局\=“@+id/seekBar”。我不理解填充部分。。你在说什么?谢谢。我的意思是矩形不会填满整个屏幕(周围是白色)。哦,那。。在xml中的相对布局标记上,删除paddingLeft、paddingRight、paddingTop和paddingBottom。干杯:)你太棒了。谢谢:)