如何在安卓中创建可变数量的文本视图

6
希望这不是一个不好的问题,但是我已经在S.O.搜索过了,没有找到答案。
我正在创建一个安卓应用程序,它本质上是一个闹钟。我希望主活动能够显示所有已创建的闹钟及一些有关这些闹钟的信息。我的问题是如何根据已创建的闹钟数量创建相应数量的文本视图。例如,如果用户已经创建(且未删除)5个闹钟,如何让它显示5个文本视图而不仅仅是一个硬编码的文本视图数量?以下是我可怕的硬编码原型,以测试功能(除了这个问题)。
<RelativeLayout 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"
    tools:context=".Launch" >

    <ScrollView
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical" 
                      android:layout_width="fill_parent" 
                      android:layout_height="fill_parent">

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>


        </LinearLayout>

    </ScrollView>


</RelativeLayout>

正如你所看到的,这里只有三个硬编码文本视图,而不是由用户创建的多个文本视图。

再次说明,我希望这不是一个太深入的问题或者已经在其他地方有了答案,但我搜索了一下,没有找到任何相关内容。

提前感谢!


您需要通过编程方式来完成,而不仅仅是通过xml。 - Steve P.
你可以使用ListView和CursorAdapter来代替ScrollView。我的意思是,如果你将闹钟保存在数据库中,那么你可以使用ListView和CursorAdapter来显示闹钟。 - user1525382
看看我的回答是否有帮助。由于您是 SO 的新手,请查看 tour - Steve P.
1个回答

9
您可以通过编程来实现这一点:
int size = numAlarms; // total number of TextViews to add

TextView[] tv = new TextView[size];
TextView temp; 

for (int i = 0; i < size; i++) 
{
    temp = new TextView(this);

    temp.setText("Alarm: " + i); //arbitrary task

    // add the textview to the linearlayout
    myLinearLayout.addView(temp);

    tv[i] = temp;
}

1
为了确保我理解正确,使用您建议的编程解决方案时,我是否还需要在代码中较早地以编程方式添加线性布局,或者可以通过某种方式将其添加到XML引用中?或者,我可以使用以下代码检索我的XML线性布局:LinearLayout linearLayout =(LinearLayout)findViewById(R.id.IdToBeSetInXml); - The Dude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接