Java 可以在不设置活动的ContentView的情况下获取对视图的引用

Java 可以在不设置活动的ContentView的情况下获取对视图的引用,java,android,android-view,android-relativelayout,android-inflate,Java,Android,Android View,Android Relativelayout,Android Inflate,我希望能够以与以下相同的方式获取对相对性yout的引用 RelativeLayout relative=(RelativeLayout)findViewById(R.id.relative) 但不设置活动的内容视图。我认为这可能与Infalter的有关?我的最终目标是获取它并将其添加为自定义视图对象的子视图。是的,您可以使用LayoutInflater。例如: LayoutInflater inflater = (LayoutInflater)getSystemService(Context.L

我希望能够以与以下相同的方式获取对
相对性yout
的引用

RelativeLayout relative=(RelativeLayout)findViewById(R.id.relative)


但不设置
活动的
内容视图
。我认为这可能与Infalter的
有关?我的最终目标是获取它并将其添加为自定义视图对象的子视图。

是的,您可以使用LayoutInflater。例如:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
View mv = inflater.inflate(R.layout.yourlayout, null);
然后你就可以

RelativeLayout relative = (RelativeLayout)mv.findViewById(R.id.relative);