Custom View extending the View-Class but still based on a XML-Layout

淺唱寂寞╮ 提交于 2019-12-11 12:59:30

问题


I want to build my own custom view which should look like the Crysis-GUI.

At first I designed a XML-based Layout and made it visible via the setContentView(int resid)-Method. Worked pretty well.

But now I wan't to go a step further and draw in my Layout. So I created a new Class, let it extend View and overrode the onDraw()-Method. So far so good. Works as expected

public class RifleView extends View {

public RifleView(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint p = new Paint();
    p.setARGB(255, 255, 0, 0);
    canvas.drawText("Hello World", 20, 20, p);
}

}

But how can I still use my XML-Layout? I can't do setContentView anymore, so how could the same effect be achieved?


回答1:


Why can't you use setContentView ? Just make an xml tag like that : <com.mycompany.mypackage.myComponent ... xml attributes an tags </com.mycompany.mypackage.myComponent>



来源:https://stackoverflow.com/questions/3502773/custom-view-extending-the-view-class-but-still-based-on-a-xml-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!