2014-06-23 2 views
0

한 번에 하나의 버튼을 클릭하여 프로그래밍 방식으로 한 번에 하나씩 클릭하면 TextViews가 서로 중첩되지 않고 기존 RelativeLayout 에 여러 TextView를 추가합니다.. -이 같은RelativeLayout에 프로그래밍 방식으로 여러 TextViews를 추가하는 방법


내가 노력하고 뭔가 다음 코드는에서 onCreate() 메소드 내부에 존재하는 다음 TextViews는 RelativeLayout의에 추가되고 있습니다

TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1); 

RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1); 

Button button1 = (Button) findViewById(R.id.button1); 
button1.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams 
        (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

     TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this); 
     newTextView.setText("text you want"); 
     rLayout1.addView(newTextView, lparams); 

    } 

,하지만 그들은 하나의 상단에 있습니다 다른 하나는 어떻게 해결하나요?

+0

스택 오버플로는 프로그래밍 관련 질문입니다. 귀하의 질문은 무엇인가? FAQ 스타일의 항목을 작성하려고하면 "질문"의 모든 내용이 답변에 포함되어야하며 질문은 문제를 설명하는 실제 스택 오버 플로우 관련 질문이어야합니다. – CommonsWare

+0

btw - CommonsWare의 의견이 유효합니다. 내 원래 게시물은 질문을 직접 충분히 묻지 않았다. 이후 게시물을 편집했다 ... – probability

답변

0

왜 app을 실행하기 전에 XML 파일에 모든 텍스트보기를 원하는만큼 많이 추가하지 않습니까? 처음에는 "Gone"에 표시하고 싶지 않은 textviews에 대한 가시성을 설정 한 다음 버튼 클릭 리스너에서 textview의 가시성을 "View.Visible"로 계속 변경하십시오. 버튼을 누를 때마다 새로운 문자보기가 나타나도록 버튼을 설정하고 카운터가 증가 할 때마다 원하는 문자보기의 가시성을 View.Visible로 변경하십시오. 내가하는 말을 이해했다면 코드를 직접 만들 수 있습니다.

+0

나는이 방법을 좋아하지만 활동에 너무 많은 textView가있는 경우 응용 프로그램이 느려질 수 있다고 우려했다. 그럴 거니? 또한, 나는 얼마나 많은 textViews 사용자가 만들지 모르겠다. – probability

+1

음 .. 확실한 것은 확실합니다. 그것은 애플 리케이션을 천천히 실 거예요. 그러나 textviews의 수가 알려지지 않은 경우 문제가됩니다 (나 또한 새로운 것임). 미안해 너를 도와 줄 수 없어! –

+0

당신의 답변이 도움이되었습니다. (제가 새로운 투표로 투표 할 수 없습니다), 감사합니다. – probability

1

한 번에 하나의 버튼을 클릭하고 기존 RelativeLayout에 여러 TextView를 추가하고 TextView를 서로 겹치지 않고 프로그래밍 방식으로 목표하는 것이 목표입니다.

여기가 마침내 나왔습니다.이 방법이 효과적 일 수는 있지만 최선의 방법 (또는 좋은 방법)이라면 확실하지 않습니다. 명심해야 할

// Creates a variable to keep track of the amount of TextViews, the 
// first TextView, an array, and then stores the TextView in the Array 
int numberOfTextViews = 1; 
TextView[] textViewArray = new TextView[20]; 
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1); 
textViewArray[numberOfTextViews - 1] = textViewToSeeFirst; 

// Also need to reference the RelativeLayout we are putting TextViews in 
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1); 

Button button1 = (Button) findViewById(R.id.button1); 
button1.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams 
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     lparams.addRule(RelativeLayout.RIGHT_OF, textViewArray[numberOfTextViews - 1].getId()); 
     lparams.addRule(RelativeLayout.ALIGN_TOP, textViewArray[numberOfTextViews - 1].getId()); 

     TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this); 
     newTextView.setText("text you want"); 
     newTextView.setId(numberOfTextViews); 
     textViewArray[numberOfTextViews] = newTextView; 
     rLayout1.addView(textViewArray[numberOfTextViews], lparams); 
     numberOfTextViews = numberOfTextViews + 1; 

    } 

어떤 것들은 :


다음 코드에서 onCreate() 메소드 내부에 존재하는 RelativeLayout.LayoutParams에 대한

  • 매개 변수가 중요하다, developer material on these parameters 참조 . WRAP_CONTENT는 TextViews가 전체 부모가 아닌 텍스트의 크기 만 차지하기 때문에 필자의 요구에 맞게 선택되었습니다 ... 내가 이것을 변경하기 전에 겹침이 발생했습니다.

  • 각각의 새로운 텍스트 뷰의 id는 새로운 레이아웃 나중에 참조 할 경우 설정

  • 는 ID가 양수를해야 매개 변수 및 0 긍정적되지 않습니다해야

  • RelativeLayout의이 TextViews을 잡고 그들을 처리되면, textViewArray는 각각의 텍스트 뷰의 ID를 저장하고 해당 XML이 안에 그것을 위해 가고있다

필요하다면 참조 할 수있다 주 부모 : 제 RelativeLayout의 및 텍스트 뷰 모두 ID를 가지고

 <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".2" > 

     <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"     
       android:text="@string/die_one" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/name_a_button" /> 
    </RelativeLayout> 

알림이 때문에이를 참조 할 수있는 활동에 addRule() 방법이다.

이 코드를 사용하면 버튼을 클릭하고 RelativeLayout에 새로운 TextViews를 겹치지 않고 추가 할 수 있습니다.

0

선형 레이아웃을 상대 레이아웃 대신 세로로 사용할 수 있습니다. 모든 텍스트 뷰를 수직으로 정렬합니다. xml 파일에 많은 수의 textviews를 추가하는 것을 고려하지 않습니다. 사용자가 버튼을 클릭하는 횟수를 알 수 없으므로 유효한 해결책입니다.

관련 문제