Combine Two ContentView's

2019-07-25 17:17发布

I am currently writing an app that will contain buttons and drawings. What I would like to do is combine the two onto the one screen.

setContentView(R.layout.activity_login_page);
setContentView(drawView);

}

class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.GREEN);
    }
    @Override
    public void onDraw(Canvas canvas) {

         super.onDraw(canvas);

         paint.setStyle(Paint.Style.FILL_AND_STROKE);
         canvas.drawCircle(50, 100, 200, paint);
         canvas.drawLine(20, 10, 50, 20, paint);

    }
}
}

When I set the content view to R.layout.activity_login_page I only get the button that I have added to the XML Layout file. When I have the two layouts being set as above, I only get the circle and the line appear and not the button. How can I combine the two?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-25 18:16

in order to use your newly customized view within a layout, you need to do the following:

  1. create all of the needed CTORs for your custom view class, or at least the one that includes the context and the attributes.

  2. in the layout file (the xml file in res/layout) put the new view as a tag that includes its full path, or use the UI designer and drag and drop it (it's in the "custom views" category).

  3. if you wish to handle your own customized attributes, you need to learn a bit more of it, and add attributes in the attr.xml file in the res/values folder, and also check the values in the CTOR (of the custom view).

查看更多
登录 后发表回答