Getting the width of a Dynamic TextView inside a Table Row
I am stuck up here trying to get the width of a TextView in a Table Row after setting some random text to it. I am using the getWidth(), but it returns zero always. Is there a way to find out the width of a dynamically created TextView.
Here is my code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
customer_id=(Tex开发者_JAVA百科tView)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+"");
}
Ok actually the layout is inflated but not yet drawn so in order to get width you will have to
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
System.out.println("...111Height..."+textview.getMeasuredWidth());
}
精彩评论