2011-09-28 47 views
1

我在這裏試圖在表格行中獲取TextView的寬度設置一些隨機文本後。 我使用的是getWidth(),但它始終返回零。有沒有辦法找出動態創建的TextView的寬度。獲取表格行內動態TextView的寬度

這是我的代碼。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    customer_id=(TextView)findViewById(R.id.customer_id); 
    comapny_name=(TextView)findViewById(R.id.compnay_name); 
    line1=(ImageView)findViewById(R.id.line1); 




    TableLayout tl=(TableLayout)findViewById(R.id.linlay); 
    TableRow tr1 =null; 
    for(int i=0;i<20;i++) 
    { 
     tr1 = new TableRow(this); 
    tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
    TextView textview = new TextView(this); 
    textview.setText("data"); 
    textview.setTextColor(Color.YELLOW); 
    textview.setTextSize(30); 


    TextView textview1 = new TextView(this); 
    textview1.setText("data1"); 
    textview1.setTextColor(Color.YELLOW); 
    textview1.setTextSize(30); 

    tr1.addView(textview); 
    tr1.addView(textview1); 
    tl.addView(tr1); 

int width=textview.getWidth(); 
Log.i("Width",width+""); 

}  
+0

? – ingsaurabh

+0

基於它的寬度,我必須調整其標題(標題) –

回答

1

確定實際的佈局膨脹,但尚未得出這樣才能得到寬度爲什麼你想,你將不得不

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
    System.out.println("...111Height..."+textview.getMeasuredWidth()); 
} 
+0

是的。你是對的。謝謝你的幫助 –