简体   繁体   English

即使调用setWillNotDraw(false),也不会在自定义视图中调用onDraw

[英]onDraw not being called in custom view even though setWillNotDraw(false) is called

So i have a custom view called Square which I want to add in a table. 所以我有一个名为Square的自定义视图,我想在表格中添加它。 When I replace my squares with text views it works perfectly. 当我用文本视图替换正方形时,它可以完美工作。

I searched quite a bit online and found that I should call setWillNotDraw(false) in the constructor. 我在网上搜索了很多内容,发现应该在构造函数中调用setWillNotDraw(false)。 I did this to no result. 我没有这样做。

Here is my class Square: 这是我的班级广场:

public class Square extends View
{
    private String courseName;
    private String className;
    private String place;
    private Weekdays weekday;
    private int hour;
    private Paint paint;

    public Square(Context context, AttributeSet attrs){
        super(context,attrs);
        setWillNotDraw(false);
    }
    public Square(Context context, String courseName, String className, String place, Weekdays weekday, int hour)
    {
        super(context);

        this.courseName = courseName;
        this.className = className;
        this.place = place;
        this.weekday = weekday;
        this.hour = hour;

        setWillNotDraw(false);

        paint = new Paint();
    }

    public Square(Context context,Weekdays weekdays, int i) {
        super(context);
        this.weekday = weekdays;
        this.hour = i;

        setWillNotDraw(false);

        paint = new Paint();
        paint.setColor(Color.BLACK);
    }

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

        canvas.drawText("Hello I'm a test", 1, 30, paint);

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


    }

I have tried setting the onMeasure() method to use 10,10 or whatever, but nothing shows up. 我尝试将onMeasure()方法设置为使用10,10或其他任何值,但是没有任何显示。 I don't know if this is relevant to my problem. 我不知道这是否与我的问题有关。

The table is made dynamically like this: 该表是动态创建的,如下所示:

public class MainActivity extends Activity {

private Controller controller;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    controller = new Controller(this);

    createTable();

}


private void createTable() {

     // get a reference for the TableLayout
    TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

    /**Create 8 rows*/
    for(int i=0;i<Controller.TABLE_ROW_COUNT;i++){
        // create a new TableRow
        TableRow row = new TableRow(this);

         /** create 5 columns*/
        for(int j=0;j<Controller.TABLE_COLUMN_COUNT;j++){
            Square sq = controller.tableModel.getSquare(Weekdays.values()[j], i);
            row.addView(sq, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        }
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    }

}

Thanks! 谢谢!

What's your target SDK version? 您的目标SDK版本是什么? If it's >= 11, hardware acceleration is making GPU cache your view. 如果> = 11,则硬件加速将使GPU缓存您的视图。 Try calling setLayerType(View.LAYER_TYPE_SOFTWARE, null) in your constructor. 尝试在构造函数中调用setLayerType(View.LAYER_TYPE_SOFTWARE, null)

Replace your onMeasure with the following: 将您的onMeasure替换为以下内容:

public void onMeasure(int wms, int hms){
    setMeasuredDimension(MeasureSpec.getSize(wms), MeasureSpec.getSize(hms));
}

The default (super) implementation in View sets its size to 0x0. View中的默认(超级)实现将其大小设置为0x0。

You may need to adjust the values to get a nice size depending on the parent. 您可能需要调整值以根据父级获得合适的大小。

If Hardware Acceleration is TRUE for your Activity. 如果您的活动的硬件加速为TRUE。 Then try disabling the Hardware acceleration for that ImageView. 然后尝试为该ImageView禁用硬件加速。

imageview.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

Worked for me. 为我工作。

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

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