简体   繁体   English

具有类的Android布局XML文件

[英]Android Layout XML file with class

I created a class that extends RelativeLayout. 我创建了一个扩展RelativeLayout的类。 I also created a xml layout file. 我还创建了一个xml布局文件。

How can I 'connect' between them as I connect Activities with their xml files ( setContentView(R.layout.main); ) 当我将Activity与它们的xml文件( setContentView(R.layout.main); )连接时,如何在它们之间“连接”

Thanks! 谢谢!

I mean you can use LayoutInflater to load the xml in the relative layout, but am not sure that what you should be doing. 我的意思是您可以使用LayoutInflater在相对布局中加载xml,但是不确定您应该做什么。 I am not sure what your trying to do but your wrapping your layout inside another view by doing this. 我不确定您要做什么,但是您可以通过这样做将布局包装在另一个视图中。 When you can just load the xml straight into your view using the same tactic. 当您可以使用相同的策略将xml直接加载到视图中时。 So you lose one level of complexity. 因此,您失去了一种复杂性。

  LayoutInflater inflater = (LayoutInflater)getContext().getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
  View view = inflater.inflate(R.layout.view,null);
  addView(view);

You can inflate the layout using the LayoutInflater right in your constructor. 您可以在构造函数中使用LayoutInflater来使布局膨胀。

public class RelSub extends RelativeLayout {
        public RelSub(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.main, this);

        }

    }

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

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