每次我設置的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);
但這些工作。
你嘗試用tvin.invalidate(); –