2016-10-02 68 views
0

當我點擊主活動中的按鈕時,需要在垂直滾動條內動態創建垂直線性佈局。線性佈局必須具有主活動編輯文本中指示的行數。Android:創建一個包含可變元素的片段

我現在如何創建帶線性佈局的片段和按下按鈕時的滾動,但我不知道如何使用按鈕來填充此線性佈局,具體取決於主活動的編輯文本的值。

圖形的解釋:

enter image description here

我有這樣的代碼:

public void onAdd(View view) { // This is the method called when button is clicked 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    BlankFragment fragment = new BlankFragment(); 
    transaction.add(R.id.table2,fragment); 
    transaction.commit(); 
} 

這是片段的XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.xxxxx.yyyyy.BlankFragment"> 

    <ScrollView 
     android:id="@+id/sv1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:id="@+id/ll6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <!-- Buttons go here --> 

     </LinearLayout> 
    </ScrollView> 

+0

非常感謝:) !! – Sergio

+0

它可以與ListView或RecyclerView。 LinearLayout只是您需要使用自定義適配器實現列表的佈局格式 – androidXP

回答

0

而不是在LinearLayout內部手動添加視圖,請使用RecyclerView或ListView並創建一個呈現視圖的簡單適配器。之後,您需要做的就是根據用戶從EditText的輸入更新適配器的項目。

相關問題