我试图在我的EditText
视图中增加一个视图。RelativeLayout
Activity
但是遇到错误时说:
指定的孩子已经有一个父母。您必须先在孩子的父母上调用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>
我已经解决了问题,只需将LayoutParams设置为searchBox,然后再将其添加到RelativeLayout即可。并且它的工作正常。
谢谢大家的回复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句