0

我有一个activity.java文件,其中有我的setContentView(R.layout.x);现在,我有一个文件,我必须y.xml在我的视图Linear Layout中附加一个onclick()方法。附件onclick()必须在我的activity.java文件中,如何包含y.xml.

我试过这个,

1.     layout = (LinearLayout) findViewById(R.layout.y);
       eView = (EditText)layout. findViewById(R.id.editview);

2.      eView = (EditText)findViewById(R.id.editview); 

但两者都给出了我的null pointer exception,我如何包括我的editText

更新

final LayoutInflater lyInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        showLinearLayout = (LinearLayout) lyInflater.inflate(R.layout.y, null);
         showView = (EditView) showLinearLayout.findViewById(R.id.edittext);
4

5 回答 5

0

您可以使用ViewGroup 的addView方法。

addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
于 2013-01-11T06:10:27.820 回答
0

LayoutInflater用于将布局 XML 文件实例化为其对应的视图对象。换句话说,它将 XML 文件作为输入并从中构建视图对象。

在您的场景中,您必须使用LayoutInflater. 阅读这篇文章

于 2013-01-10T03:53:15.773 回答
0

您可以使用通货膨胀,如下所示:

final LayoutInflater lyInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout yLayout = (LinearLayout) lyInflater .inflate(
                R.layout.y, null);
eView = (EditText)yLayout.findViewById(R.id.editview);

所以你不会再有异常了。希望能帮助到你。

于 2013-01-10T03:41:02.200 回答
0

内存。如果要将yxml 文件包含到xxml 文件中,请按照以下步骤操作。

我假设您想在单击按钮或其他方法时包含Linear Layout在您Activity的单击方法中,然后将其添加到您的xml 文件中并在开始时添加,因此您无法显示线性布局。onclick()Linear Layoutxandroid:visibility="gone"

 <LinearLayout
        android:id="@+id/history_value_body"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >    <<<<<<<<<<<   HERE

 ----------------------------
 -----------------------------
 </LinearLayout>

现在,从 java 类中使它在需要时可见,在您的情况下进入onclick方法。喜欢...

linear.setVisibility(View.VISIBLE);   // linear is the object of your Linearlayout

如果有任何问题然后问我。

祝你好运。

于 2013-01-10T04:03:42.317 回答
0

如果我正确理解了这个问题,那么您的 Activity 使用x.xml,并且您还希望包含在 中定义的另一个布局y.xml

您可以使用<merge>or<include>标记来执行此操作,如文档中所述

或者,您可以使用ViewStub在布局中的给定位置有条件地扩展另一个布局。例如,您可以在 中包含一个ViewStub标签,并在视图层次结构中的同一位置进行x.xml膨胀。y.xml然后,您可以附加您需要的任何点击侦听器(通过使用findViewById())。

于 2013-01-10T04:08:05.207 回答