Android 未调用自定义 onDraw() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3343912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:49:22  来源:igfitidea点击:

Custom onDraw() method not called

androiddrawing

提问by mynameisanthpny

<LinearLayout android:id="@+id/svLL" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/sv"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <!--
            <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/scrollbar_2_text" />
        -->
        <com.mypackage.MyDrawableView
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>


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));
    }

} 

I am putting a breakpoint, but breakpoint is never hit inside onDraw()method, what's wrong ?

我放置了一个断点,但断点从未在onDraw()方法内部被击中,怎么了?

回答by Dragan Marjanovi?

Add setWillNotDraw(false)in MyDrawableView constructor. Original answer is here.

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

回答by Romain Guy

Your View has a height of 0. You set your View to have height=wrap_content, but your don't override onMeasure()to tell the UI toolkit how big your View is.

您的 View 的高度为 0。您将您的 View 设置为 have height=wrap_content,但您不要覆盖onMeasure()以告诉 UI 工具包您的 View 有多大。

回答by Gust Jc

You need to @Overridethe OnMeasure(int, int)method to specify the size of your view by calling setMeasuredDimension(int,int).

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

If you don't, the default method will set your view to have size width=0 height=0. And therefore don't even try to call onDrawat all.

如果不这样做,默认方法会将您的视图设置为 size width=0 height=0。因此,甚至根本不要尝试打电话onDraw

The OnMeasuremethod calls setMeasuredDimension(int,int)which tells how big is your view.

OnMeasure方法调用setMeasuredDimension(int,int)告诉您视图有多大。

(So you need to get the size of the android screen on the OnMeasuremethod and call setMeasuredDimensionaccordingly).

(所以你需要在方法上获取android屏幕的大小OnMeasure并相应地调用setMeasuredDimension)。

回答by virajdasondi

Just make layout_widthand layout_heightof the custom view some fix at initial time instead of wrap_contentor fill_parent. That worked for me.

只是要layout_widthlayout_height定制的浏览初始时刻一些修复,而不是wrap_contentfill_parent。那对我有用。

回答by Biplab Kundu

Though I am new in android, still I believe the problem is because of

虽然我是 android 新手,但我仍然相信问题是因为

setContentView(R.layout.xmllayout); 

Instead of "R.layout.xmllayout", pass MyDrawableViewobject (using findViewById) and check. I have not tried, but hope, it will work.

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

回答by Muhammad Aamir Ali

You need to use invalidate() in constructor method to get the onDraw Called.

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

回答by John Great

After you set the custom view width, you should set scroll width.

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

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(); 

回答by CoolMind

Probably

大概

setMinimumHeight(width);
setMinimumWidth(height);

in constructor are not set or onMeasure()is not overriden.

在构造函数中未设置或未onMeasure()覆盖。

回答by Suragch

Here is one more reason that your view might not be appearing. If you are creating your custom ViewGroup with subviews, remember to add the subviews to your ViewGroup.

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

In your ViewGroup constructor call

在您的 ViewGroup 构造函数调用中

addView(mySubView);

Also remember to measure and layout your subviews in onLayout()

还要记住测量和布局你的子视图 onLayout()

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mySubview.measure(...);
    mySubview.layout(...);
}