CustomView不会填充可用空间

时间:2017-05-03 15:36:20

标签: android android-custom-view

我正在尝试创建一个可以填充屏幕上所有可用空间的视图。我将我的视图的布局参数设置为match_parent,并在getHeight()上绘制getWidth()时使用RectCanvas并且它仍然仅填充约三分之二的<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:beat_box="http://schemas.android.com/tools"> <com.yotam.aprivate.demo.mybeatbox.Views.BeatBox android:layout_width="match_parent" android:layout_height="match_parent" beat_box:my_color="5D32EA"> </com.yotam.aprivate.demo.mybeatbox.Views.BeatBox> </RelativeLayout> 屏幕。

public class BeatBox extends View {
private int color;
private Context context;
private Paint paintBox = new Paint();
private Paint paintBackground = new Paint();

public BeatBox(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    color = getAttrColor(attrs);
    initDrawing();
}
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawRect(0,0,canvas.getHeight(), canvas.getWidth(), paintBackground); //this.getHeight() this.getWidth() also doesn't work
    paintBackground.setShader(new LinearGradient(0, 0, getWidth() , getHeight(), Color.parseColor("#cb356b"), Color.parseColor("#bd3f32"), Shader.TileMode.MIRROR));
    super.onDraw(canvas);
}

CustomView:

    TASKS
    id
    parent_id
    completed
    name

    EMPLOYEES
    id
    name

    EMPLOYEE_TASKS
    id
    employee_id
    task_id

It doesn't fill the whole layout, I want it to fill the white area

2 个答案:

答案 0 :(得分:3)

您在此行中反转了宽度和高度:

canvas.drawRect(0,0, canvas.getWidth(),canvas.getHeight(), paintBackground); 

答案 1 :(得分:1)

canvas.getHeight()方法的参数如下:

right

您已将canvas.getWidth()用作bottomheight用作custom view,这就是width canvas.getWidth()与设备{{}相同的原因1}}。

<强> SOLUTION:

您应将right用作canvas.getHeight(),将bottom用作onDraw()

更新@Override protected void onDraw(Canvas canvas) { canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paintBackground); paintBackground.setShader(new LinearGradient(0, 0, getWidth() , getHeight(), Color.parseColor("#cb356b"), Color.parseColor("#bd3f32"), Shader.TileMode.MIRROR)); super.onDraw(canvas); } 方法,如下所示:

{
    "Myobj" : 
    {
        "mychild1":"this is my child 1",
        "mychild2":"this is my second child",
        "Myarr":[3,4,5,6,7],
        "mychild3": 
        {
            "myarr2":["A","B","C"],
            "myinnerChild2": 
            {
                "key1":"val1",
                "key2":"val2"
            }
        }
    }
}

希望这会有所帮助〜