2012-10-17 71 views
0

每次我設置的TextView的佈局參數去包裹內容或填寫父母它不是在視圖中顯示,但是當我將它設置爲像素它的工作這是爲什麼?查看佈局不顯示時的LayoutParams設定編程

下面是相關代碼:

items = (LinearLayout) findViewById(R.id.llmain); 
     tb = new TableLayout(MainActivity.this); 

     tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     tb.setGravity(Gravity.CENTER); 


      TableRow tr = new TableRow(MainActivity.this); 
      ImageButton ima = new ImageButton(MainActivity.this); 
      ImageButton imr = new ImageButton(MainActivity.this); 
      TextView tvin = new TextView(this); 

      TableRow.LayoutParams trp = new TableRow.LayoutParams(); 
      trp.setMargins(0, 0, 0, 10); 
      tr.setLayoutParams(trp); 
      tr.setGravity(Gravity.CENTER); 

      ima.setBackgroundColor(Color.TRANSPARENT); 
      ima.setImageResource(R.drawable.add); 

      imr.setBackgroundColor(Color.TRANSPARENT); 
      imr.setImageResource(R.drawable.remove); 

      tvin.setWidth(100); 
      tvin.setHeight(45); 
      tvin.setGravity(Gravity.CENTER); 
      tvin.setText(sil[i]); 
      tvin.setBackgroundColor(Color.rgb(200, 0, 255)); 

      tr.addView(ima); 
      tr.addView(tvin); 
      tr.addView(imr); 

      tb.addView(tr); 

     items.addView(tb); 

我用這個

tvin.setLayoutParams(new LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 

試圖和這個

tvin.setWidth(LayoutParams.FILL_PARENT); 
tvin.setHeight(LayoutParams.FILL_PARENT); 

但這些工作。

+0

你嘗試用tvin.invalidate(); –

回答

4

嘗試:

tvin.setLayoutParams(new TableRow.LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT)); 

並刪除tvin.setWidth和高度。

你應該總是設置視圖的母公司的TableRow的佈局PARAMS在這種情況下。 MATCH_PARENT沒有什麼區別,但它是一個新的名字,你應該使用它來代替FILL_PARENT的。

+0

謝謝!有用... – philip

1

在這條線u必須使用高度和寬度.. TableRow.LayoutParams trp = new TableRow.LayoutParams();像這樣

TableRow row = new TableRow(context); 
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT, 
       android.widget.TableRow.LayoutParams.WRAP_CONTENT)); 
1

嘗試使用LinearLayout.LayoutParams

TextView tvin = new TextView(this); 
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    tvin.setLayoutParams(llp);