Android:使用可变元素

时间:2016-10-02 22:03:25

标签: android button scroll fragment android-linearlayout

当我点击主活动中的按钮时,我需要在垂直滚动内动态创建垂直线性布局。线性布局必须具有主活动的编辑文本中指示的行数。

我现在如何创建具有线性布局的片段和按下按钮时的滚动,但我不知道如何使用按钮填充此线性布局,具体取决于主要活动的编辑文本的值。

图解说明:

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>

1 个答案:

答案 0 :(得分:0)

不是手动在LinearLayout中添加视图,而是使用RecyclerView或ListView创建一个呈现视图的简单适配器。之后,您需要做的就是根据用户在EditText中的输入更新您的适配器项目。