2014-10-17 70 views
3

我的代碼出錯了。我有我的代表片段的佈局的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> 

,並代表一個觀點,即穿鞋走片段佈局內(element.xml)另一個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方法內:

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種元素)。爲什麼沒有顯示其他元素?怎麼了?

+0

您也可以將您的root_layout包裝在滾動視圖中。 – 2014-10-17 08:28:59

回答

3

問題是android:layout_height="match_parent",在你的RelativeLayout中。它應該是wrap_content

+0

我的錯誤,非常感謝。太多的工作,對我來說,幸運的是這是星期五! – giozh 2014-10-17 08:31:22

+0

哈哈,不客氣 – Blackbelt 2014-10-17 08:31:51

相關問題