【问题标题】:Create View-Object from XML file in android从 android 中的 XML 文件创建 View-Object
【发布时间】:2012-01-18 20:39:46
【问题描述】:

我只想从 xml 布局文件中获取一个对象,而不必将其实现到当前布局中。

我知道方法

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);

但在执行上述操作后,布局将立即实施并显示在我的“myparent”视图中,对吧?我只想让对象本身来获取它的属性等。也许(但只是也许)稍后将其插入显示的布局中。 这可能吗?

问候

【问题讨论】:

  • 你的意思是什么类型的对象?如果您只想要 XML 定义的 GUI 对象的句柄,为什么不直接使用 findViewById(id)?

标签: android android-layout layout-inflater


【解决方案1】:

你应该把你的行改为:

LayoutInflater.from(context).inflate(R.layout.myfile, null);

您可以在文档 here 中找到它。

【讨论】:

    【解决方案2】:
    LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);
    

    end参数决定是否自动将新视图添加到myparent。将其设置为 false 以仍使用父级的布局属性。

    或者,如果您不关心父母的布局参数,请按照@inazaruk 的回答

    【讨论】:

      【解决方案3】:

      您可以通过以下方式使该组件不可见:

      android:visibility="gone"
      

      来源: http://developer.android.com/reference/android/view/View.html#attr_android:visibility

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多