添加自定义的矩形到自定义的RelativeLayout [英] Adding a custom rectangle to a custom RelativeLayout

查看:154
本文介绍了添加自定义的矩形到自定义的RelativeLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的RelativeLayout,在这里我要动态地创建矩形。

他们不是在我目前的code所示,但我不知道什么原因。

自定义的RelativeLayout:

 公共类DrawView扩展RelativeLayout的{    公共DrawView(上下文的背景下){
        超级(上下文);
        setFocusable(真);        this.addView(新的Rectangle(this.getContext()));
    }    @覆盖
    保护无效的onDraw(帆布油画){
        super.onDraw(画布);    }

自定义矩形:

 公共类矩形扩展视图{    公共矩形(上下文的背景下){
        超级(上下文);
    }    公共矩形(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公共矩形(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }    @覆盖
    保护无效的onDraw(帆布油画){
        super.onDraw(画布);
        涂料粉刷=新的油漆();
        paint.setColor(Color.WHITE);        canvas.drawRect(新的Rect(),漆);    }}

编辑:
解决办法是:

 公共类矩形扩展视图{    公共矩形(上下文的背景下){
        超级(上下文);
        在里面();
    }    公共矩形(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }    公共矩形(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        在里面();
    }    私人无效的init(){
        this.setBackgroundResource(R.drawable.rectangle);    }    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);        INT宽度= 150;
        INT高度= 50;        setMeasuredDimension(宽度,高度);
    }}
< XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>
    <角落
        机器人:半径=5SP/>
    <梯度
        机器人:角=315
        机器人:startColor =#FFFF6666
        机器人:ENDCOLOR =#FFFF1111
        机器人:TYPE =线性
        />
< /形状>


解决方案

我认为你缺少设置你的形状的的LayoutParams 。尝试寿创建一个长方形对象,设置其宽+高,然后将其添加到您的 RelativeLayout的:D

I have a custom RelativeLayout, where i want to dynamically create Rectangles.

They arent shown in my current code, but i dont know whats the reason.

Custom RelativeLayout:

public class DrawView extends RelativeLayout {

    public DrawView(Context context) {
        super(context);
        setFocusable(true);

        this.addView(new Rectangle(this.getContext()));
    }

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

    }

Custom Rectangle:

public class Rectangle extends View {

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

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);

        canvas.drawRect(new Rect(), paint);

    }

}

EDIT: Solution was:

public class Rectangle extends View {

    public Rectangle(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        this.setBackgroundResource(R.drawable.rectangle);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 150;
        int height = 50;

        setMeasuredDimension(width, height);
    }

}


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="5sp"/>
    <gradient
        android:angle="315"
        android:startColor="#FFFF6666"
        android:endColor="#FFFF1111"
        android:type="linear"
        />
</shape>

解决方案

I think you are missing to set your Shape's LayoutParams. Try tho create a Rectangle object, set its width + height and then add it to your RelativeLayout :D

这篇关于添加自定义的矩形到自定义的RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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