2011-09-28 48 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

是的。你是对的。谢谢你的帮助 –