【发布时间】:2011-07-20 00:06:25
【问题描述】:
Android 自定义组件有问题。尝试绘制椭圆形,但没有任何反应。
我在布局 xml 文件中有这一行
<android.project.realtimedata.DemoView android:id="@+id/demoView"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
这是我的自定义组件类的代码。
package android.project.realtimedata;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
public class DemoView extends View{
ShapeDrawable thisGauge = null;
public DemoView(Context context){
super(context);
init();
}
public DemoView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
private void init(){
int x = 10;
int y = 10;
int width = 300;
int height = 50;
thisGauge = new ShapeDrawable(new OvalShape());
thisGauge.getPaint().setColor(0xff74AC23);
thisGauge.setBounds(x, y, x + width, y + height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
thisGauge.draw(canvas);
}
}
我在Activity的onCreate方法中也有这一行
demoView = (DemoView) findViewById(R.id.demoView);
每当我启动应用程序时,自定义组件都不存在。 我尝试从 LogCat 中查看它,它肯定是被创建的。 我在这里错过了什么?
提前致谢。
【问题讨论】:
-
你在“包装”什么内容?如果您在自定义视图中没有对象/文本,是否有可能只是包装零高度的内容,从而导致高度为零的椭圆?尝试将 xml layout_height 设置为 fill_parent 看看是否有任何改变?
-
是的。你是对的。非常感谢。这解决了问题。
-
我可以将此作为答案发布并谦虚地请求您接受吗? :)
标签: android components