简体   繁体   English

将视图分配给RelativeLayout

[英]Assign a View to a RelativeLayout

I want to draw a path that is confined to just a single RelativeLayout , not the entire layout. 我想画一条只限于单个RelativeLayout而不是整个布局的路径。 So I have my DrawPath Class like this: 所以我有这样的DrawPath类:

public class DrawPath extends View {
Paint paint = new Paint();
Path path = new Path();
...

And then in my Main Activity I have this bit of code: 然后在我的主要活动中,我有这段代码:

RelativeLayout gB_RL = (RelativeLayout)findViewById(R.id.rel_RL);

dp = new DrawPath(this);
dp.setBackgroundColor(Color.WHITE);
// setContentView(dp);

I've commented out setContentView(dp); 我已经注释掉setContentView(dp); because that would assign the DrawPath class to the entire layout. 因为这会将DrawPath类分配给整个布局。 But what I can't figure out is how to assign DrawPath to just the gB_RL Relative Layout 但是我不知道如何将DrawPath仅分配给gB_RL相对布局

All ViewGroup subclasses (such as RelativeLayout ) have a collection of addView() methods that you can use to add a new View to that ViewGroup. 所有ViewGroup子类(例如RelativeLayout )都具有addView()方法的集合,可用于将新View添加到该ViewGroup。

In this case, your call can be as simple as gB_RL.addView(dp) . 在这种情况下,您的调用可以像gB_RL.addView(dp)一样简单。

Since you are using a RelativeLayout, you probably want to also create an instance of RelativeLayout.LayoutParams and set that on your dp View so that you can align the dp View correctly. 由于您使用的是RelativeLayout,因此您可能还想创建一个RelativeLayout.LayoutParams实例,并将其设置在dp视图上,以便可以正确地对齐dp视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM