Android:无法在版面中膨胀EditText

Android:无法在版面中膨胀EditText,android,Android,我正在尝试在我的活动中的RelativeLayout中膨胀编辑文本视图 但得到一个错误说明: 指定的子级已具有父级。必须首先对子级的父级调用removeView() 我的代码: RelativeLayout relLayout= new RelativeLayout(insideActivity); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MA

我正在尝试在我的
活动
中的
RelativeLayout
中膨胀
编辑文本
视图

但得到一个错误说明:

指定的子级已具有父级。必须首先对子级的父级调用removeView()

我的代码:

RelativeLayout relLayout= new RelativeLayout(insideActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,           RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.setLayoutParams(params);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.holo_edit_text, null);
EditText searchBox = (EditText)view.findViewById(R.id.holo_text);

relLayout.addView(searchBox);
这里是holo_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText 
        android:id="@+id/holo_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true" />

</RelativeLayout>

您的holo\u edit\u text.xml应该是

<?xml version="1.0" encoding="utf-8"?>
    <EditText 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/holo_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
     />
在添加到相对布局时是否尝试过此操作

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    relLayout.addView(view, params);
试试这个

View child_view = LayoutInflater.from(this).inflate(R.layout.holo_edit_text, null);
relLayout.addView(child_view);

使用try/catch机制

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (rootView != null) {
                ((ViewGroup) rootView.getParent()).removeView(rootView);
            }
              try {
               rootView = inflater.inflate(R.layout.your_layout_file_here, container,false);
               EditText searchBox = (EditText)rootView.findViewById(R.id.holo_text);
               relLayout.addView(searchBox);
              } catch (InflateException e) {

              }
            return rootView;
        }

我已经修复了我的问题,只需在将LayoutParams添加到RelativeLayout之前将其设置为searchBox。它工作得很好


谢谢大家的回复。

请记录例外情况。为什么要手动插入?使用XML布局描述。或者将膨胀视图直接添加到布局中。@user527759我的完整布局将在“我的活动”中动态创建。而且我不能以编程方式向Edittext添加样式。仅在xml中可以向Edittext添加样式。因此,我只从xml扩展EditText。并使其膨胀。编辑文本搜索框=充气器。充气(R.layout.holo\u编辑文本,空);relLayout.addView(搜索框);我已经试过你提到的所有这些了。但没有显示任何内容。尝试过,但不起作用。也尝试了getLayoutInflater(),但没有成功
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (rootView != null) {
                ((ViewGroup) rootView.getParent()).removeView(rootView);
            }
              try {
               rootView = inflater.inflate(R.layout.your_layout_file_here, container,false);
               EditText searchBox = (EditText)rootView.findViewById(R.id.holo_text);
               relLayout.addView(searchBox);
              } catch (InflateException e) {

              }
            return rootView;
        }