2011-09-10 2 views
1

테이블에 하나의 행만 정의하려고합니다. 이 행에, 나는 다음과 같은 폭으로, 3 열을 추가하려면 :프로그래밍 방식으로 TableLayout 내에서 행의 열 가중치를 할당하십시오.

textView1 :

viewDivider 내용으로 넓게해야한다 : 2 픽셀 폭

TextView2해야한다 : 나머지 영역을 차지한다 tablerow의.

그러나 프로그래밍 방식으로 위의 레이아웃을 달성 할 수 없습니다. 여기에 코드입니다 :

public class Copy_2_of_Test extends Activity { 
TableLayout tableLayout = null; 
TextView textView1 = null; 
TextView textView2 = null; 
View viewDivider = null; 

TableLayout tab = null; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView (R.layout.test); 
     tableLayout = (TableLayout)this.findViewById(R.id.mainTable); 
     addViews(); 
     } 

     private void addViews() { 
     // table row with layout params of type TableLayout.LayoutParams 
     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.FILL_PARENT)); 

     // Textview 1 with layout params of type TableRow.LayoutParams 
     textView1 = new TextView(this); 
     textView1.setText("Value1"); 
     textView1.setTextColor(Color.BLACK); 
     textView1.setBackgroundColor(Color.YELLOW); 
     textView1.setLayoutParams(new TableRow.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.FILL_PARENT)); 
     tr.addView(textView1); 

     viewDivider = new View (this); 
     viewDivider.setLayoutParams(new TableRow.LayoutParams(
          2, 
          LayoutParams.FILL_PARENT)); 
     viewDivider.setBackgroundColor(Color.MAGENTA); 
     tr.addView(viewDivider); 


     // Textview 2 with layout params of type TableRow.LayoutParams 
     textView2 = new TextView(this); 
     textView2.setText("Value2 Value2 Value2 Value2 "); 
     textView2.setTextColor(Color.BLACK); 
     textView2.setBackgroundColor(Color.YELLOW); 
     textView2.setLayoutParams(new TableRow.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.FILL_PARENT)); 
     tr.addView(textView2); 

     // Add row to TableLayout. 
     tableLayout.addView(tr,new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    } 

    } 

그리고 여기 test.xml의의 :

 <?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="*" 
     android:background="@color/black" 
     android:id="@+id/mainTable"> 

    </TableLayout> 

나는 위의 레이아웃 thefollowing 문제에 직면하고있다 :

1) textView1 그 내용이 더 폭을 차지합니다.

2) viewDivider 폭을 많이 차지하고 도움을

감사를 2 픽셀을 좁혀되지 않습니다.

답변

0

시도해보십시오. 폭을 설정하기 위해 setWidth() 메소드를 사용하십시오.

For Reference

관련 문제