android自定义View的显示问题。。在线等
为什么我的自定义View不通过XML文件直接setContentView可以显示的出来但是通过布局文件调用时View在界面上显示不出来?
**************************自定义View***************
public class MyImageView extends TextView {
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(70, 70, 30, paint);
this.invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
****************************XML布局文件********************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.xx.MyImageView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.xx.MyImageView>
</LinearLayout>
****************************Activity*****************************
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bingtu);
}
}