BST*_*aal 18 android textview android-layout layoutparams
我有一个EditText,一个Button和一个TextView.单击该按钮时,textview显示以edittext编写的文本.是否可以根据文本找到占用的textview的大小.即如果它有三个字符" abc ",现在是什么宽度,如果它有5个字符,如" abcde ",那么宽度是多少?
小智 62
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();
Run Code Online (Sandbox Code Playgroud)
要么
textView.setText("bla");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)
小智 10
请试试这个:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView edit = (TextView) findViewById(R.id.edit);
edit.setTextSize(20);
edit.setText("Hello, world");
edit.measure(0, 0);
int width = edit.getMeasuredWidth();
Log.w("width", width.toString());
}
Run Code Online (Sandbox Code Playgroud)
在获得宽度之前,您必须测量视图/标签/文本编辑.如果这不起作用,请告诉我.
小智 6
TextView txt = new TextView(mContext);
txt.setText("Some Text)";
int height = txt.getLineCount() * txt.getLineHeight();
int width = txt.getWidth();
Run Code Online (Sandbox Code Playgroud)