2016-10-02 64 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的输入更新适配器的项目。

相关问题