2014-12-10 4 views
0

그래서 "Add Row"버튼을 누를 때마다 동적으로 테이블 행을 추가하는 애플리케이션이 있습니다. 첨부 된 코드는 "행 추가"를 누를 때 실행되는 코드입니다. 내가 뭘 하려는지 같은 행에 서로 옆에 3 EditTexts가 있습니다. 그렇게하기 위해 각 EditText의 Layout_Width를 변경하여 첫 번째 EditText가 다른 두 가지를 화면에서 자르지 않도록해야합니다. 나는 이것을 제대로 수행 할 수있는 방법을 찾지 못했고 누군가가 나를 도와 주는지 궁금해하고있었습니다. 이를 수행하는 방법을 파악한 후 다음 단계는 화면 크기에 따라 layout_width를 조정하는 것이지만 나중에 도로를 조정하는 것입니다. 사용자가 이론적으로 원하는만큼의 행을 가질 수 있기 때문에 프로그래밍 방식으로 수행해야합니다.프로그래밍 방식으로 안드로이드에서 EditText의 layout_width를 설정하십시오.

private OnClickListener btnListener = new OnClickListener() 
    { 

     public void onClick(View v) 
     { 

      tablerow = new TableRow(getActivity()); 

      ed1 = new EditText(getActivity()); 
      ed1.setInputType(InputType.TYPE_CLASS_TEXT); 

      ed2 = new EditText(getActivity()); 
      ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); 

      ed3 = new EditText(getActivity()); 
      ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); 

      tablerow.addView(ed1); 
      tablerow.addView(ed2); 
      tablerow.addView(ed3); 
      table1.addView(tablerow); 

     } 

    }; 
+0

try ed1.getLayoutParams(). width = 32; – aadi53

+0

이 작업을 수행 할 때마다 LA NullPointerException이 발생합니다. Edittext는 현재 어떤 이유로 너비가 없기 때문에 가정합니다. –

+0

전에 layoutParams를 설정해야합니다. 너비 설정. – aadi53

답변

2

나는 당신이 찾고있는 것이 LayoutParams와 "weight"값이라고 믿습니다.

시도 다음의 LayoutParams 생성자

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f); 

ed1 = new EditText(getActivity()); 
ed1.setInputType(InputType.TYPE_CLASS_TEXT); 
ed1.setLayoutParams(params); 

세 번째 값 (1.0F)가 그 TableRow EditTexts 모두 동일한 폭을 종료한다 .. 중량이다.

+0

정확한 너비를 지정하고 싶지는 않습니다. LayoutParams 너비 값과 1.0f 가중치의 MATCH_PARENT는 EditText 너비를 자동으로 조정하여 3 개의 크기가 모두 같고 결합 된 너비가 MATCH_PARENT 너비로 정의되어 있으므로 너비가 화면 너비에 맞습니다. –

+0

이것은 올바른 경로에 있지만 완전히 작동하지는 않습니다. 모든 EditText를 화면에 표시하지만 어떤 이유로 Ed1이 화면의 90 %를 제공하고 ed2/ed3이 나머지 10 %를 공유합니다. 내 테이블의 너비가 MATCH_PARENT입니다. –

+0

이걸 가지고 노는 것은 내 XML 파일의 레이아웃과 관련이 있습니다. 당신의 솔루션은 매력처럼 작동했습니다. 정말 고마워! LayoutParams와 관련이 있다는 것을 알고 있지만 어떻게해야하는지 알 수 없었습니다! –

0

다음 접근 방식을 시도해 볼 수 있습니다. xml을 기반으로합니다. 레이아웃이 table_row.xml 인 곳에서 놀 수 있습니다.

public class MainActivity extends ActionBarActivity implements View.OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.a_mn_button_add_row: 
       onClickButtonAddRow(); 
       break; 
     } 
    } 

    private void onClickButtonAddRow() { 
     TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table); 
     tableLayout.addView(View.inflate(this, R.layout.table_row, null)); 
    } 
} 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <Button 
     android:id="@+id/a_mn_button_add_row" 
     android:onClick="onClick" 
     android:text="Add row" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TableLayout 
      android:id="@+id/a_mn_table" 
      android:stretchColumns="0,1,2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </ScrollView> 

</LinearLayout> 

table_row.xml 3-1-2 비율로 사용 사례

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" /> 

    <EditText 
     android:inputType="text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:inputType="text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</TableRow> 

table_row.xml.

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:layout_weight="3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:inputType="text" /> 

    <EditText 
     android:layout_weight="1" 
     android:inputType="text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:layout_weight="2" 
     android:inputType="text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 

</TableRow> 
+0

필자는이 글이 쓰여진 방식을 좋아하기 때문에이 솔루션을 사용해 보니 매우 기뻤습니다. 내 응용 프로그램에서는 "tableLayout.addView (View.inflate (this, R.layout.table_row, null));" MainActivity에서 오류가 발생했습니다. 이것은 ActionBarActivity의 조각을 확장하기 때문입니다. 응용 프로그램을 실행할 수있는 유일한 방법은 "this"대신 "getActivity()"를 사용하여 해당 줄을 호출하는 것이지만 NullPointerException 때문에 응용 프로그램이 충돌합니다. 그래도 도와 ​​줘서 고마워! 이것은 우아 해 보인다. –

+0

@JacobMoultonRhodes 예, 조각 사용을위한 코드를 조정해야하기 때문에 충돌이 발생합니다. – gio

관련 문제