绘制多个自定义视图 [英] Drawing multiple custom views
问题描述
我想绘制多个自定义视图的布局,但只有第一个观点我想补充它的绘制。
I'm trying to draw multiple custom views in a layout but only the first view I add it's drawn.
我的自定义视图类是:
public ButtonView(Context context, int k) {
super(context);
this.setX(20+k*80);
init();
}
private void init(){
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0x99ffffff);
}
@Override
protected void onDraw(Canvas cv){
cv.drawText(""+getX(), 0, 80, paint);
}
我显示的x文字的坐标,以便知道哪些视图绘制。
I'm displaying the text of the x coordinate in order to know which view is drawn.
这是的onCreate
我的主要活动中的code。
This is the code of onCreate
of my main activity.
blay = (LinearLayout) this.findViewById(R.id.buttonslayout);
for(int k=0; k<10; k++){
ButtonView e = new ButtonView(this,k);
blay.addView(e);
}
当我只运行的第一个视图绘制应用程序。我得到一个20.0在正确的位置显示为白色,但意见的其余未显示。通过调试应用程序,我认识到的意见实例被创建,但由于某种原因,他们不会显示。我也试过用无效
和覆盖 onMeasure
方法,但仍然没有工作。
When I run the application only the first view is drawn. I get a "20.0" displayed in white in the correct position, but the rest of the views are not showing. By debugging the app I realize that the instances of the views are created but for some reason they are not displayed. Also I tried to use invalidate
and override onMeasure
method but still not working.
推荐答案
我已经修改自定义视图,请检查是否这是working.I与code猜主要的问题是一个视图占据了整个屏幕。所以我重写onMeasure功能
I have modified your custom view please check if this is working.I guess the main problem with your code was that one view was occupying the whole screen. So I overridden onMeasure function
package com.example.stack2;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class ButtonView extends View{
public ButtonView(Context context, int k) {
super(context);
//this.setX(20+k*80);
init();
}
public ButtonView(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// TODO Auto-generated constructor stub
}
public ButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public ButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ButtonView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
Paint paint;
private void init(){
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//e.measure(0, 0);
getLayoutParams().width=100;
}
@Override
protected void onDraw(Canvas cv){
super.onDraw(cv);
cv.drawText(""+getX(), 0, cv.getHeight()/2, paint);
}
}
另外,请确保您的线性布局的方向是水平
Also make sure the orientation of your linear layout is horizontal
这篇关于绘制多个自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!