Custom onDraw() method not called
<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
// Call开发者_如何学Pythoned 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 ?
Add setWillNotDraw(false)
in MyDrawableView constructor. Original answer is here.
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.
You need to @Override
the OnMeasure(int, int)
method to specify the size of your view by calling 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 onDraw
at all.
The OnMeasure
method calls setMeasuredDimension(int,int)
which tells how big is your view.
(So you need to get the size of the android screen on the OnMeasure
method and call setMeasuredDimension
accordingly).
Just make layout_width
and layout_height
of the custom view some fix at initial time instead of wrap_content
or fill_parent
.
That worked for me.
You need to use invalidate() in constructor method to get the onDraw Called.
Probably
setMinimumHeight(width);
setMinimumWidth(height);
in constructor are not set or onMeasure()
is not overriden.
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.
In your ViewGroup constructor call
addView(mySubView);
Also remember to measure and layout your subviews in onLayout()
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mySubview.measure(...);
mySubview.layout(...);
}
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();
Though I am new in android, still I believe the problem is because of
setContentView(R.layout.xmllayout);
Instead of "R.layout.xmllayout"
, pass MyDrawableView
object (using findViewById
) and check.
I have not tried, but hope, it will work.
精彩评论