Aad*_*ada 8 android width textview
我正在尝试向textview添加一个文本,我已将其宽度设置为Wrap_content.我试图获得此textview的宽度.但它在所有情况下都显示为0.在将文本设置到文本视图后,如何获得textview的宽度.
代码如下:
LinearLayout ll= new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ll.addView(tv);
tv.setText("Hello 1234567890-0987654321qwertyuio787888888888888888888888888888888888888888888888888");
System.out.println("The width is == "+tv.getWidth());// result is 0
this.setContentView(ll);
Run Code Online (Sandbox Code Playgroud)
请建议.提前致谢.
Con*_*nst 10
只有在布局过程完成后,动态宽度/高度的视图才能获得正确的大小(http://developer.android.com/reference/android/view/View.html#Layout).
您可以将OnLayoutChangeListener添加到TextView并在那里获得它的大小:
tv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
final int width = right - left;
System.out.println("The width is == " + width);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14618 次 |
最近记录: |