2011-10-04 219 views
0

我已經嘗試了很多方法,但它似乎不適用於我。以編程方式設置寬度Imageview

我的代碼: TableLayout tTopic=(TableLayout) findViewById(R.id.__topic);

TableRow tr = new TableRow(this); 
    TableLayout.LayoutParams lpt2=null; 
    lpt2 = new TableLayout.LayoutParams(
      TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT); 
    lpt2.setMargins(4, 2, 4, 2); 

    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    ImageView view = new ImageView(this); 
    view.setLayoutParams(new LayoutParams(20,30)); 

    view.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    view.setImageResource(R.drawable.kotak); 


    tr.addView(view); 
    tTopic.addView(tr,lpt2); 

爲什麼它沒有出現?但是當我省略setLayoutParams時,它顯示爲默認大小。感謝您的幫助

+0

使用'TableRow.LayoutParams' –

回答

4

試試這個:

view.setLayoutParams(new TableRow.LayoutParams(20,30)); 
1

要設置ImageView的寬度編程做到這一點:

view.getLayoutParams().width = 20; 
相關問題