简体   繁体   English

在Android中如何获取设置为Wrap_Content的Textview的宽度

[英]In Android how to get the width of the Textview which is set to Wrap_Content

I am trying to add a text to the textview for which i have set the width as Wrap_content. 我正在尝试向textview添加一个文本,我已将其宽度设置为Wrap_content。 I am trying to get the width of this textview. 我试图获得此textview的宽度。 But its showing 0 in all the cases. 但它在所有情况下都显示为0。 How Can i get the width of the textview after setting the text into it. 在将文本设置到文本视图后,如何获得textview的宽度。

the code is as: 代码如下:

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

Please suggest. 请建议。 Thanks in advance. 提前致谢。

Views with the dynamic width/height get their correct size only after a layout process was finished ( http://developer.android.com/reference/android/view/View.html#Layout ). 只有在布局过程完成后,动态宽度/高度的视图才能获得正确的大小( http://developer.android.com/reference/android/view/View.html#Layout )。
You can add OnLayoutChangeListener to your TextView and get it's size there: 您可以将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);                
    });

You can not get the width of a View with dynamic size before the layout is completely built. 在完全构建布局之前,无法获得具有动态大小的View的宽度。 That means there is no way you can get it in onCreate(). 这意味着你无法在onCreate()中获得它。 One way would be to create a class that inherits from TextView and overrides onSizeChanged() . 一种方法是创建一个继承自TextView并覆盖onSizeChanged()

When are you calling this? 你什么时候打电话给这个? Has it already been drawn to the screen? 它已被吸引到屏幕上吗?

It sounds like you are calling getWidth() too early. 听起来你太早调用getWidth()了。

You can also take a look at this question . 你也可以看看这个问题

this works for me: 这对我有用:

RelativeLayout.LayoutParams mTextViewLayoutParams = (RelativeLayout.LayoutParams) mTextView.getLayoutParams();
mTextView.setText(R.string.text);
mTextView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = mShareTip.getMeasuredWidth();
//use the width to do what you want
mShareTip.setLayoutParams(mShareTipLayoutParams);

you can try this: 你可以试试这个:

textView.measure(0,0);
int width = textView.getMeasuredWidth();

hello use this method: 你好用这个方法:

textview.post(new Runnable() {
    @Override
    public void run() {
        int width = textview.getWidth();
        int height = textview.getHeight();
        textview.setText( String.valueOf( width +","+ height ));
    }

}); });

source: https://gist.github.com/omorandi/59e8b06a6e81d4b8364f 来源: https//gist.github.com/omorandi/59e8b06a6e81d4b8364f

You can use this library to schedule the task of perform calculation on the width to the correct time after the view had been completely drawn 在完全绘制视图后,您可以使用此库将宽度执行计算的任务计划到正确的时间

https://github.com/Mohamed-Fadel/MainThreadScheduler https://github.com/Mohamed-Fadel/MainThreadScheduler

Sample of use: 使用样品:

MainThreadScheduler.scheduleWhenIdle(new Runnable() {
       @Override
       public void run() {
           int width = textview.getWidth();
           int height = textview.getHeight();
           textview.setText( String.valueOf( width +","+ height ));
       }
   });

You should use this: 你应该用这个:

textView.getMeasuredWidth(); textView.getMeasuredWidth();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将较大的文本设置为具有宽度wrap_content的textview? - How to set bigger text into textview with width wrap_content? Kotlin 如何获取 wrap_content 的宽度 TextView - Kotlin how get width of wrap_content TextView Android - 获取设置为wrap_content的按钮的宽度 - Android - getting the width of a button which is set to wrap_content 如何在RelativeLayout中的TextView右对齐,其layout_width为wrap_content并且其父布局具有wrap_content - How to align right a TextView in RelativeLayout which layout_width is wrap_content and it's parent layout has wrap_content to 使用layout_width =“ wrap_content”获取TextView的可用宽度 - Get available width for TextView with layout_width=“wrap_content” 将 TextView 宽度设置为 wrap_content 的 LinearLayout 裁剪子项 - LinearLayout clipping children with TextView width set to wrap_content 如何设置textview的宽度wrap_content但限制为父级宽度的1/3 - how to set textview width wrap_content but limit to 1/3 of parent's width 文本在TextView中被截断,宽度设置为wrap_content - Text being truncated in TextView with width set to wrap_content 如何检测具有 wrap_content 属性的 TextView 的宽度? - How to detect width of TextView that has attribute wrap_content? 宽度为“wrap_content”的多行TextView - Multiline TextView with width “wrap_content”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM