2016-10-02 1 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からのユーザーの入力に基づいて、アダプターの項目を更新するだけです。

関連する問題