我们可以在Custom View中使用布局吗?

时间:2010-06-17 12:32:27

标签: android

朋友,

我有自定义视图类

public class Zoom extends View {
        private Drawable image;
        private int zoomControler=20; 
        public Zoom(Context context)
        {
            super(context);
            image=context.getResources().getDrawable(R.drawable.icon);
            setFocusable(true);

        }
        @Override
        protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);

            //TransparentPanel tp = new TransparentPanel(this.getContext());

            //Button MyButton = new Button(this.getContext());
            //MyButton.setText("Press me");

            //tp.addView(MyButton);

            image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
            image.draw(canvas);




        }

}

我可以在此自定义视图中使用SetContentView(R.layout.mylayout)来显示该设计吗?

我如何用图像提取方法显示按钮,因为我已对代码进行了评论?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您必须在res/layout目录中创建自定义XML:

<?xml version="1.0" encoding="utf-8"?>
<your.package.Zoom xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

如果您想使用R.layout.mylayout,则必须调用XML文件mylayout.xml

相关问题