【发布时间】:2010-07-27 13:08:34
【问题描述】:
<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));
}
}
我设置了一个断点,但在onDraw() 方法中从未命中断点,这是怎么回事?
【问题讨论】:
-
你的Activity的代码是什么?也许问题在那里?
-
public class MyActivity extends Activity { // 你的成员变量声明在这里 // 首次创建活动时调用。 @Override public void onCreate(Bundle savedInstanceState) { // 你的代码在这里 super.onCreate(savedInstanceState); setContentView(R.layout.xmllayout); LinearLayout svll = (LinearLayout) findViewById(R.id.svLL); svll.setLayoutParams(new LinearLayout.LayoutParams(300, 300)); } }
-
我把你的代码移到问题里了,这样更容易理解
-
感谢klez,还没有人发现错误,一定是一些简单的错误:(
-
有趣,我覆盖了 onLayout 和 onMeasure。在这些函数中遇到断点。但是对于 onLayout,底部为 0,右侧为 300,左侧为 0,顶部为 0。这是不调用 onDraw 的原因吗?没有使用xml的customview示例代码吗?