android监听ondraw方法,android-自定义onDraw()方法未调用

android-自定义onDraw()方法未调用

android:layout_width="fill_parent" android:layout_height="fill_parent">

android:layout_width="wrap_content" android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android">

android:layout_width="fill_parent" android:layout_height="wrap_content" />

public class MyDrawableView extends View {

Context thisContext;

public MyDrawableView(Context context, AttributeSet attr) {

super(context);

thisContext = context;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

final Paint paint = new Paint();

paint.setColor(Color.BLUE);

paint.setTextSize(12);

canvas.drawText("Blah blah", 0, 100, paint);

}

}

public class MyActivity extends Activity {

// Your member variable declaration here

// Called when the activity is first created.

@Override

public void onCreate(Bundle savedInstanceState) {

// Your code here super.onCreate(savedInstanceState);

setContentView(R.layout.xmllayout);

LinearLayout svll = (LinearLayout) findViewById(R.id.svLL);

svll.setLayoutParams(new LinearLayout.LayoutParams(300, 300));

}

}

我设置了一个断点,但是在onDraw()方法内部从未打断点,这是怎么回事?

9个解决方案

92 votes

在MyDrawableView构造函数中添加setWillNotDraw(false)。 原始答案在这里。

Dragan Marjanović answered 2020-08-01T21:08:51Z

36 votes

您的视图的高度为0。您将视图设置为具有299269372892361616256,但不要覆盖onMeasure()来告诉UI工具包您的视图有多大。

Romain Guy answered 2020-08-01T21:09:11Z

15 votes

您需要OnMeasure setMeasuredDimension方法通过调用setMeasuredDimension(int,int)来指定视图的大小。

如果您不这样做,则默认方法会将您的视图设置为299269406544101414784。因此,甚至根本不要尝试调用setMeasuredDimension。

OnMeasure方法调用setMeasuredDimension,它告诉您视图的大小。

(因此,您需要在OnMeasure方法上获取android屏幕的大小,并相应地调用setMeasuredDimension)。

Gust Jc answered 2020-08-01T21:09:45Z

3 votes

只需在初始时间使自定义视图的layout_width和layout_height而不是wrap_content或fill_parent固定即可。那对我有用。

virajdasondi answered 2020-08-01T21:10:05Z

0 votes

虽然我是android的新手,但我仍然认为问题出在

setContentView(R.layout.xmllayout);

代替"R.layout.xmllayout",传递MyDrawableView对象(使用findViewById)并检查。我没有尝试过,但希望它能起作用。

Biplab Kundu answered 2020-08-01T21:10:29Z

0 votes

您需要在构造函数方法中使用invalidate()来获取onDraw。

Muhammad Aamir Ali answered 2020-08-01T21:10:49Z

0 votes

设置自定义视图宽度后,应设置滚动宽度。

FMChart = (HKFMDrawChart)findViewById(R.id.fm_chart);

FMChart.setWidth(xxxx);

float scrollViewWidth = FMChart.getScrellViewWidth();

FMChart.setLayoutParams(new LinearLayout.LayoutParams(

(int) (scrollViewWidth + HKApplication.getScreenWidth() / 2),

LinearLayout.LayoutParams.WRAP_CONTENT));

FMChart.postInvalidate(); // onDraw();

John Great answered 2020-08-01T21:11:09Z

0 votes

大概

setMinimumHeight(width);

setMinimumWidth(height);

在构造函数中未设置或未重写onMeasure()。

CoolMind answered 2020-08-01T21:11:33Z

0 votes

这是您的视图可能不显示的另一个原因。 如果要使用子视图创建自定义ViewGroup,请记住将子视图添加到ViewGroup中。

在您的ViewGroup构造函数调用中

addView(mySubView);

还记得在onLayout()中测量和布置子视图

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

mySubview.measure(...);

mySubview.layout(...);

}

Suragch answered 2020-08-01T21:12:02Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值