如何在android中以编程方式查找TextView的文本区域(高度/宽度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24359538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:03:37  来源:igfitidea点击:

How to find the Text Area(Height/Width) of TextView programmatically in android

androidandroid-layouttextviewlayoutparams

提问by BST Kaal

I have an EditText, a Buttonand a TextView. On clicking the button, textview shows the text written in edittext. Is it possible to find the size of textview occupied depending upon text. i.e. If It has three characters "abc", what is width now, if it has 5 characters like "abcde" , then what is the width ?

我有一个EditText,一个Button和一个TextView。单击按钮时,textview 会显示以 edittext 编写的文本。是否可以根据文本找到占用的 textview 的大小。即如果它有三个字符“ abc”,那么现在宽度是多少,如果它有 5 个字符像“ abcde”,那么宽度是多少?

回答by pigeongram

Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();

or

或者

textView.setText("bla");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();

回答by Iosif

Please try this:

请试试这个:

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());
}

Before you get width, you have to measure the view / label / text edit. Please let me know if this is not working.

在获得宽度之前,您必须测量视图/标签/文本编辑。如果这不起作用,请告诉我。

回答by MilapTank

please tell me width in??? do you want ?

请告诉我宽度???你想要 ?

TextViewmethod getWidth()gives you width of your view, in pixels

TextView方法getWidth()为您提供视图的宽度,以像素为单位

TextView textView = (TextView)findViewById(R.id.textview);
textView.getWidth(); //width of your view, in pixels 

回答by Monir Khlaf

TextView txt = new TextView(mContext);
txt.setText("Some Text)";
int height = txt.getLineCount() *  txt.getLineHeight();
int width = txt.getWidth();

回答by Haresh Chhelana

Try this way,hope this will help you to solve your problem.

试试这个方法,希望这能帮助你解决你的问题。

yourTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
     int width = yourTextView.getMeasuredWidth();
     int height = yourTextView.getMeasuredHeight();

   }
});

回答by Shanu Singh

Well, I haven't converted this code into swift4 syntax, but the logic will remain the same. This is an extension method for Xamarin.ios(C#).

好吧,我没有将此代码转换为 swift4 语法,但逻辑将保持不变。这是 Xamarin.ios(C#) 的扩展方法。

To Calculate the Height

计算高度

 public static nfloat GetEstimateHeight(this UITextView textView, UIView View)
    {
        var size = new CoreGraphics.CGSize(View.Frame.Width, height: float.PositiveInfinity);
        var estimatedSize = textView.SizeThatFits(size);
        return estimatedSize.Height;
    }

To Calculate the Width

计算宽度

    public static nfloat GetEstimateWidth(this UITextView textView, UIView View)
    {
        var size = new CoreGraphics.CGSize(View.Frame.Width, height: float.PositiveInfinity);
        var estimatedSize = textView.SizeThatFits(size);
        return estimatedSize.Width;
    }

The logic here that will work for swift is

这里适用于 swift 的逻辑是

var size = new CoreGraphics.CGSize(View.Frame.Width, height: float.PositiveInfinity);
        var estimatedSize = textView.SizeThatFits(size);
        var textViewFinalHeight = estimatedSize.Height;