2011-11-03 2 views
0

프로그래밍 방식으로 다음 레이아웃을 복제하려고합니다. 그러나 setGravity 및 setPadding은 영향을 미치지 않는 것 같습니다.SetPadding 및 SetGravity를 사용한 프로그래밍 방식의 레이아웃

TableRows 내에서 textviews를 다시 만들려고합니다 (패딩이 있고 두 번째 텍스트보기가 오른쪽 정렬 됨). 또한 동적이므로 추가 항목을 다시 만들려고합니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/myinfo_root" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:scrollbars="vertical" 
     > 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainTable" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> 

    <TableRow> 
     <TextView 
      android:layout_column="1" 
      android:text="Text1" 
      android:padding="3dip" /> 

     <TextView 
      android:id="@+id/text1" 
      android:text="TBD" 
      android:gravity="right" 
      android:padding="3dip" /> 
    </TableRow> 
    <TableRow> 
     <TextView 
      android:layout_column="1" 
      android:text="Text2" 
      android:padding="3dip" /> 

     <TextView 
      android:id="@+id/text2" 
      android:text="TBD" 
      android:gravity="right" 
      android:padding="3dip" /> 
    </TableRow> 
    . . . 

다음은 TextViews를 추가하는 코드 단편입니다. 작동하는 것처럼 보이지만 항목에 여백이없고 오른쪽 정렬되지 않습니다.

int n = 0; 
    TableLayout table = (TableLayout) findViewById(R.id.mainTable); 

    for (int j=0; j<2; j++) { 
     Log.v(LOG_TAG, "entering for loop"); 
     n++; 

     TableRow tr = new TableRow(getApplicationContext()); 
     tr.setId(100+n); 

     String key = "hello"; 
     float value = (float) 1.388); 
     value = value/3600; 

     TextView tvDynamicKey = new TextView(getApplicationContext());      
     TextView tvDynamicUnit = new TextView(getApplicationContext()); 

     tvDynamicUnit.setText(Float.toString(value)); 
     tvDynamicUnit.setGravity(Gravity.RIGHT); 
     tvDynamicKey.setText(key + " " + n); 
     tvDynamicKey.setPadding(3, 3, 3, 3); 
     tvDynamicUnit.setPadding(3, 3, 3, 3); 
     tr.addView(tvDynamicKey); 
     tr.addView(tvDynamicUnit); 
     table.addView(tr); 
    }  
    } 

답변

1

레이아웃에서 항상 왼쪽 요소 (android : layout_column = "1")에 column1을 지정합니다. 그러나 동적 생성을 위해 프로그래밍 방식으로하지는 않습니다. 이런 식으로 할 수 있습니다. 나는 그것을 시험했다. 그것은 내 환경에서 작동한다.

tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1)); 
+0

감사합니다. – user836200

관련 문제