【问题标题】:Android Layout XML file with class带有类的 Android 布局 XML 文件
【发布时间】:2011-12-27 12:45:32
【问题描述】:

我创建了一个扩展RelativeLayout 的类。 我还创建了一个 xml 布局文件。

当我将活动与它们的 xml 文件连接时,我如何在它们之间“连接” (setContentView(R.layout.main);)

谢谢!

【问题讨论】:

  • 显示您创建的 xml 和扩展类。

标签: java android xml class android-activity


【解决方案1】:

您可以在构造函数中使用 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);

        }

    }

【讨论】:

    【解决方案2】:

    我的意思是您可以使用 LayoutInflater 在相对布局中加载 xml,但不确定您应该做什么。我不确定你想做什么,但你通过这样做将你的布局包装在另一个视图中。当您可以使用相同的策略将 xml 直接加载到您的视图中时。所以你失去了一层复杂性。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多