开发者

Android Custom Component not being displayed

Having a problem with Android Custom components. Trying to draw an oval shape but nothing happening.

I have this line in layout xml file

        <android.project.realtimedata.DemoView android:id="@+id/demoView"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />

Here is the code for my custom component class.

    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 = 1开发者_JS百科0;
            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); 
        }
    }

I also have this line in onCreate method of Activity

demoView = (DemoView) findViewById(R.id.demoView);

Whenever I launch the application the custom component is not there. I tried looking at it from LogCat and it definitely gets created. What am I missing here?

Thanks in advance.


Make sure that you calling findViewById(R.id.demoView) after calling setContentView(...). To ensure that your view is being inflated, you can call Log.d("DemoView", "Created") from inside your DemoView constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜