【问题标题】:In Android how to get the width of the Textview which is set to Wrap_Content在 Android 中如何获取设置为 Wrap_Content 的 Textview 的宽度
【发布时间】:2013-04-07 22:56:44
【问题描述】:

我正在尝试将文本添加到我已将其宽度设置为 Wrap_content 的文本视图中。我正在尝试获取此文本视图的宽度。但它在所有情况下都显示为 0。将文本设置为文本视图后,如何获取文本视图的宽度。

代码如下:

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

请提出建议。 提前致谢。

【问题讨论】:

    标签: android textview width


    【解决方案1】:

    您可以使用此库将执行宽度计算的任务安排到视图完全绘制后的正确时间

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

    使用示例:

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

    【讨论】:

      【解决方案2】:

      你好用这个方法:

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

      });

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

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案3】:

      你可以试试这个:

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

      【讨论】:

        【解决方案4】:

        这对我有用:

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

        【讨论】:

          【解决方案5】:

          你应该使用这个:

          textView.getMeasuredWidth();

          【讨论】:

            【解决方案6】:

            只有在布局过程完成后,具有动态宽度/高度的视图才能获得正确的大小 (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);                
                });
            

            【讨论】:

            • API 8 及更高版本的等效项是什么?
            • 您可以继承 TextView 并覆盖 onLayout()。从中调用 super.onLayout() 并保存 TextView 的宽度。这应该适用于任何 API 级别。
            • 类需要 API 级别 11(当前最低为 8):android.view.View.OnLayoutChangeListener
            【解决方案7】:

            在布局完全构建之前,您无法获取具有动态大小的 View 的宽度。这意味着您无法在 onCreate() 中获取它。一种方法是创建一个继承自 TextView 并覆盖 onSizeChanged() 的类。

            【讨论】:

              【解决方案8】:

              你什么时候打电话给这个?它已经被绘制到屏幕上了吗?

              听起来你打电话给getWidth() 太早了。

              您也可以使用look at this question

              【讨论】:

                猜你喜欢
                • 2021-06-04
                • 2011-12-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-02-02
                • 2011-12-25
                相关资源
                最近更新 更多