2013-06-18 4 views
5

잘하면 이건 나쁜 질문이 아니지만 S.O. 대답을 찾을 수 없었습니다.android에서 다양한 수의 textview를 만드는 방법

본질적으로 자명종 인 안드로이드 응용 프로그램을 만들고 있습니다. 주 활동이 알람에 대한 정보와 함께 생성 된 모든 알람을 표시하기를 원합니다. 이 질문에 대한 나의 질문은 얼마나 많은 알람이 생성되었는지에 따라 주어진 수의 텍스트 뷰를 생성하는 방법이다. 예를 들어, 사용자가 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> 

자세히 알 수 있듯이 사용자가 작성한 여러 텍스트보기 대신 3 가지 하드 코딩 된 텍스트보기가 있습니다.

다시 한 번 질문의 깊이가 아니거나 다른 곳에 게시 된 답변이 있지만 검색해도 찾을 수 없기를 바랍니다.

미리 감사드립니다.

+0

당신은 단지 XML을 통해 프로그래밍 방식으로이 작업을 수행해야합니다. –

+0

scrollview 대신에 listview 및 cursoradapter를 사용할 수 있습니다. db에 알람을 저장하면 cursoradapter와 함께 listview를 사용하여 알람을 표시 할 수 있습니다. –

+0

내 대답이 도움이되는지 확인하십시오. 당신이 처음이므로, [둘러보기] (http://stackoverflow.com/about)를 확인하십시오. –

답변

4

프로그래밍 방식으로이 작업을 수행 할 수 있습니다

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

정확하게 이해했는지 필자가 제안한 프로그래밍 방식 솔루션을 사용할 때 프로그래밍 방식으로 선형 레이아웃을 추가해야 할 필요가 있거나 XML 참조에 어떻게 든 추가 할 수 있습니까? 아니면 그냥 내 xml 선형 레이아웃을 검색 할 수 있습니다 :'LinearLayout linearLayout = (LinearLayout) findViewById (R.id.IdToBeSetInXml); ' –

+0

네, 그렇게 할 수 있습니다. –

관련 문제