2013-08-26 4 views
0

많은 게시물을 읽었지만 아무도 내 문제를 해결하지 않았습니다. 또 다른 재미있는 점은 에뮬레이터에서 edittext의 위치가 유지된다는 것입니다 (다른 위젯이 아래로 이동한다는 의미). 그러나 실제 휴대 전화의 동일한 구현에는 위젯의 위치가 표시되지 않습니다. . 나는 TextView팀 구성원 추가을 클릭하면 EditText 위젯을 동적으로 추가해야하는 다음과 같은 LinearLayout이 있습니다.LinearLayout에 동적으로 EditText를 추가 할 수 없습니다.

<LinearLayout 
      android:id="@+id/layout_add_task" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/separator" 
      android:layout_margin="5dp" 
      android:background="@color/yellow_background" 
      android:orientation="vertical" 
      android:padding="5dp"> 

      <LinearLayout 
       android:id="@+id/layout_task_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/iv_check_research_tasks" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon_check_empty" /> 

       <EditText 
        android:id="@+id/et_task_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:selectAllOnFocus="true" 
        android:text="Task Name" 
        android:textSize="20dp" 
        android:textStyle="bold" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/tv_add_team_member" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:layout_marginTop="5dp" 
       android:text="Add a team member" 
       android:textColor="@color/text_green" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      <LinearLayout 
       android:id="@+id/ll_add_task" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/tv_add_team_member" 
       android:orientation="horizontal" 
       android:weightSum="100" > 

       <ImageView 
        android:id="@+id/iv_create_assignment_attach" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_weight="10" 
        android:src="@drawable/icon_flag_alt" /> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="15" 
        android:text="Due" 
        android:textSize="15dp" /> 

       <EditText 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="65" 
        android:hint="None" 
        android:selectAllOnFocus="true" /> 

       <ImageView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_weight="10" 
        android:src="@drawable/icon_calendar_empty" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center_horizontal" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/btn_create_assignment_trash" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="30dp" 
        android:layout_marginTop="5dp" 
        android:text="Trash" /> 

       <Button 
        android:id="@+id/btn_create_assignment_done" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="30dp" 
        android:layout_marginTop="5dp" 
        android:text="Done" /> 
      </LinearLayout> 
     </LinearLayout> 

여기에 Java 코드 인 onClickListener TextView 구현이 있습니다.

tvAddTeamMember.setOnClickListener(new OnClickListener() { 

     @Override 

     public void onClick(View v) { 
      LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name); 
      EditText et = new EditText(getActivity()); 
      LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
      et.setLayoutParams(p); 
      et.setText("Text"); 
      ll.addView(et); 

     } 
+0

Edittext에 중력을 설정하십시오. – Piyush

+0

@PiyushGupta 중력을 설정하지 않았습니다. –

+0

좋아 .. 그래서 그냥 chk. – Piyush

답변

3

안녕하세요 @Anas Azeem : 코드가 맞습니다. 코드를 사용하여 선형 레이아웃에 editetxt를 동적으로 추가 할 수 있습니다.

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
: 문제는 레이아웃이 라인을 변경하려고
<LinearLayout 
      android:id="@+id/layout_task_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/iv_check_research_tasks" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

      <EditText 
       android:id="@+id/et_task_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:selectAllOnFocus="true" 
       android:text="Task Name" 
       android:textSize="20dp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
0

레이아웃을 추가하는 경우, 항상 ViewStub를 선언 할 수 있습니다. 그 후 당신은 단순히 viewStub를 찾아서()를 팽창 전화 :

ViewStub stub = (ViewStub) findViewById(R.id.stub); 
View inflated = stub.inflate(); 

이 안드로이드 SDK에 있음을 할 수있는 홍보 방법입니다. 덕분에 XML 파일에서 infalte 레이아웃을 정의 할 수 있습니다.

+0

답변 주셔서 감사합니다. 그러나 추가 할 Edittext의 수가 많지는 않습니다. 그렇다면 내 시나리오에서 어떻게 대답을 사용할 수 있습니까? –

+0

추가 할 수있는 가능한 많은 EditText가있는 경우 사용할 수 없습니다. – toommm

+0

당신은 각 클릭에 +1 값을주는 edittext보기의 수를 세는 카운터 변수를 선언 할 수 있습니다. 그런 다음, edittext보기의 수를 얻기 위해 카운터 변수를 사용할 수 있습니다. 편집 텍스 트에 일부 데이터가있을 수 있습니다 , 당신은 ArrayList 에 같은 것을 저장하고 같은 것을 사용할 수 있습니다. 나는 그것이 잘되기를 바란다! – inderbagga

3

이 방법을 사용할 수 있습니다! 이것은 당신이 찾고있는 것이 아닙니다. 그러나 문제는 해결 될 것입니다.

1) 기본적으로 XML에 EditText보기를 추가하여 보이지 않게합니다.

2) TextView를 클릭하여 표시합니다.

즐기십시오!

+1

추가 할 Editexts의 수는 제한되지 않습니다. –

+0

n 개의 텍스트 뷰 아래에서 n 개의 편집 텍스트 뷰를 생성해야합니까, 아니면 편집 텍스트 뷰를 수직 목록으로 사용할 수 있습니까? – inderbagga

0

이 코드를 사용해보십시오. ur line ll.addView (et)를 바꿉니다.

:: ll.addView (et, p);

및 삭제 ur line et.setLayoutParams (p);

+0

죄송합니다. 문제는 그대로입니다. –

+0

ur et_task_name edittext 너비를 ur 레이아웃 파일에서 wrap_content로 변경하고 ur 코드를 실행하십시오. – abc

1

아래처럼 대신 하나 개 사용의 "수평"등의 추가 레이아웃은 "수직"방향을 언급 only.in 레이아웃 파일에 이와

:

android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
//Then do your work 
0

이 작업을 수행하는 간단한 방법.

 Button add = (Button) findViewById(R.id.add_checkpoint); 
     final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);   
     // layout has assigned weight = "1.0" in xml & it's a child of ScrollView 
     add.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       LayoutInflater oLayoutInflater = getLayoutInflater(); 
       // oView is declared globally & Layout "row_checkpoint" contains a EditText 
       oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null); 

       final EditText speciesEditText = (EditText) oView 
         .findViewById(R.id.checkpoint); 
       speciesEditText.setId(layout.getChildCount()); 
       // Perform whatever required over EditText, same way you can add more elements. 
       layout.addView(oView); 
      } 
     }); 

희망이 있으면 도움이 될 것입니다.

관련 문제