我的代码出了点问题.我有一个表示片段布局的XML(fragment_layout.xml):
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/root_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" ></linearLayout>
另一个XML文件表示片段视图(element.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="match_parent" ><TextVIEw androID:ID="@+ID/tv" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" /><EditText androID:ID="@+ID/et" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:inputType="number" androID:text="0" /></relativeLayout>
这就是我在onCreateVIEw方法中填充Fragment布局的方式:
VIEw vIEw = inflater.inflate(R.layout.fragment_layout, container, false); some_elements = getActivity().getResources().getStringArray( R.array.some_array); linearLayout root = (linearLayout) vIEw.findVIEwByID(R.ID.root_layout); for (int i = 0; i < some_elements.length; i++) { VIEw element = inflater.inflate( R.layout.element, container, false); TextVIEw tv= (TextVIEw) element.findVIEwByID(R.ID.tv); prodotto.setText(prodotti[i]); EditText et= (EditText) element.findVIEwByID(R.ID.et); root.addVIEw(element); } return vIEw;
现在发生的是片段内仅显示“ some_elements”数组的第一个元素(该元素包含10个元素).为什么没有显示其他元素?怎么了?
解决方法:
问题是您的relativeLayout中的androID:layout_height =“ match_parent”.应该是wrap_content
总结以上是内存溢出为你收集整理的android-在Fragment中以编程方式将视图添加到另一个视图全部内容,希望文章能够帮你解决android-在Fragment中以编程方式将视图添加到另一个视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)