2013-05-16 5 views
0

버튼을 클릭 할 때 동적으로 텍스트보기를 추가하고 싶습니다. 나는 이것을 구현했지만, 이전 코드를 쓰는 것 위에 그것을 추가 할 때. 내 코드에 아무런 문제가 없나요?동적으로 여러 레이아웃 추가하기

imGbtn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       int id =0; 
       final String data = textView.getText().toString(); 
       //childHolder.title.setText(data); 
       LinearLayout ll = new LinearLayout(mContext); 
       ll.setOrientation(LinearLayout.VERTICAL); 
       ll.setId(id); 
       tvll.addView(ll); 
       // TextView tv1 = new TextView(mContext); 
       // tv1.setText("Dynamic layouts ftw!"); 
       // ll.addView(tv1); 

       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       layoutParams.setMargins(25, 20, 25, 10); 
       TextView tv= new TextView(mContext); 
       tv.setId(id); 
       tv.setText(data); 
       ll.addView(tv,layoutParams); 
       textView.setText(""); 

      } 
     }); 

답변

0

나는있는 LinearLayout에서 버튼 클릭에 텍스트 뷰를 추가 한 후 자바에서 레이아웃 xml.Create 객체의 내부 LinearLayout을 정의하고 건의 할 것입니다. 다음 코드

<LinearLayout 
      android:id="@+id/relatedChannels" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </LinearLayout> 

자바

LinearLayout cat_linear=(LinearLayout) findViewById(R.id.list_Category); 
    TextView tv = new TextView(context); 
    tv.setText(category.get(i).getNAME()); 
    tv[i][i].setLayoutParams(new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 
    cat_linear.addView(tv); 
2

가있는 LinearLayout 온 클릭

public class ViewOnClick extends Activity { 
    LinearLayout.LayoutParams layoutParams; 
    LinearLayout ll; 
    static int i; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button b = (Button)findViewById(R.id.Button01); 

     b.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 


    LinearLayout ll = new LinearLayout(mContext); 
       ll.setOrientation(LinearLayout.VERTICAL); 
       ll.setId(id); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       layoutParams.setMargins(25, 20, 25, 10); 
       EditText view = new EditText(ViewOnClick.this);    
       view.setText(++i+" view"); 
       ll.addView(view, layoutParams); 

      }}); 
    } 
} 
에 EDITTEXT를 추가
관련 문제