我正在尝试创建自定义TextView
。我也看了here以获得我的指导,并且我让它工作了,虽然我仍然对为什么没有绘制其他组件感到困惑,这比android的reference guide更简单但它不在我的工作中。
从我制作的自定义类(CustomTextView
w / c扩展TextView
),
我尝试添加OnTouchListener()
并为invalidate()
调用this
方法,其他组件w / c为TextView
但仍未绘制。
CustomTextView.java
public class CustomTextView extends TextView {
TextView extra_detail;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
extra_detail = new TextView(context);
extra_detail.setText("Hey!");
extra_detail.setPadding(20, 0, 0, 0);
extra_detail.setTextSize(2);
a.recycle();
//I also tried this in case the space isn't enough
setMinHeight(80);
extra_detail.invalidate();
this.invalidate();
}
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Here's my problem
//both are not being drawn
//even if I remove the other one and vice-versa
canvas.drawText("Hey", 10, 0, paint);
extra_detail.draw(canvas); //is this the right way to do this?
}
@Override
public boolean onTouchEvent(MotionEvent event) {
invalidate();
extra_detail.invalidate();
//still not being drawn
//after the invalidation
Toast.makeText(getContext(), "Hey!", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
extra_detail.measure(widthMeasureSpec, heightMeasureSpec);
}
}
我似乎在这里遗漏了一些东西,但无法弄清楚。
更新
我的extend
Activity
与ActionBarActivity
碰到了Activity
。现在有了这种预先定义的title bar
,实现了canvas.draw*()
,并隐藏了我在屏幕左上角所做的所有setY(10)
。我在调用canvas.drawText()
后找出它,然后View
出现。
但我仍有在onDraw()
事件中绘制另一个{{1}}的问题。