如何在Android中创建可变数量的TextViews [英] How to create a variable number of textviews in android

查看:72
本文介绍了如何在Android中创建可变数量的TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这不是一个坏问题,但是我搜索了S.O.却找不到答案.

Hopefully this isn't a bad question, however I've searched through S.O. and haven't been able to find the answer.

我正在创建一个本质上是闹钟的android应用程序.我希望主要活动显示所有已创建的警报以及一些有关警报的信息.我的问题是如何根据已创建的警报数量创建给定数量的文本视图.例如,如果用户创建(而不是删除)5个警报,我如何显示5个文本视图,而不是仅显示硬编码的文本视图?以下是我经过严格编码的原型,以测试功能(除了此挂断).

I am creating an android application that is essentially an alarm clock. I want the main activity to display all alarms that have been created along with some information about the alarms. My question about this is how to create a given number of text views based on how many alarms have been created. For example if a user has created (and not deleted) 5 alarms, how do I have it display 5 textviews rather than just a hard-coded number of text views? Following is my horribly hard-coded prototype to test the functionality (aside from this hangup).

<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>

如您所见,只有三个硬编码的文本视图,而不是用户创建的许多文本视图.

As you can see, there are just three hard-coded text views rather than a number of text views as created by the user.

再次,我希望这不是一个问题的深度,也不要在其他地方发布了答案,但是我进行了搜索,但找不到任何东西.

Again, I hope this isn't too in depth of a question or one that has an answer posted elsewhere but I searched for it and couldn't find anything.

提前谢谢!

推荐答案

您可以通过编程方式执行此操作:

You can do this programmatically:

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;
}

这篇关于如何在Android中创建可变数量的TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆