20

我以编程方式创建了 TextView,如下所示:

                TextView mTextView = new TextView(getApplicationContext());

                final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ((MarginLayoutParams) params).setMargins(8, 8, 0, 0);

                mTextView.setText(mText);
                mTextView.setLayoutParams(params);
                mTextView.setPadding(2, 2, 2, 2);
                mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));

                tagMap.addView(mTextView);

                textViewsWidth = mTextView.getWidth();

但是 mTextView.getWidth() 总是返回 0

如果我尝试:

mTextView.getLayoutParams().width

它返回 LayoutParams 类中的 LayoutParams 对应值(-1 或 -2)

如何获得视图的宽度?

编辑我需要在这里这样做:

            @Override
        public void onPostExecute(Hashtable<String, Integer> hash){
            final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
            final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
            final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
            final Enumeration<String> e = hash.keys();
            int index = 0;
            int textViewsWidth = 0;

            while(e.hasMoreElements()){
                final TextView tV = new TextView(getApplicationContext());

                tV.setTextColor(Color.parseColor("#666666"));
                tV.setText(Html.fromHtml(randomColor() + e.nextElement()));
                tV.setTextSize(arr.get(index));

                final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ((MarginLayoutParams) params).setMargins(8, 8, 0, 0);

                tV.setLayoutParams(params);
                tV.setPadding(2, 2, 2, 2);
                tV.setBackground(getResources().getDrawable(R.drawable.note_corps));

                tagMap.addView(tV);

                textViewsWidth += tV.getWidth();

                index++;
            }

            tagMapScroll.setVisibility(ScrollView.VISIBLE);

        }

编辑解决方案我从@androiduser的回答中使用了这个:

mTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                int width = mTextView.getMeasuredWidth();
                int height = mTextView.getMeasuredHeight();

问题解决了 !

4

5 回答 5

16

我使用了这个解决方案:

yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // Ensure you call it only once
        yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        // Here you can get the size :)
    }
});
于 2013-10-28T11:25:33.243 回答
4

得到这种方式:

tV.post(new Runnable() {
                    @Override
                    public void run() {
                        int width=tV.getMeasuredWidth();
                    }
                });
于 2013-10-28T11:39:18.613 回答
2

这些值是 FILL_PARENT 和 WRAP_CONTENT 的常量值。如果要检查视图大小,可以尝试以下方式:

tagMap.addView(mTextView);
tagMap.post(new Runnable() {
                    @Override
                    public void run() {
                        mTextView. getWidth();
                    }
                });

您必须等到 android 绘制TextView. 这样,您将在 tagMap 队列中发布一个 Runnable,希望在绘制 textview 之后执行(所以它应该是重量和高度)

于 2013-10-28T11:22:08.463 回答
1

在这种方法中,您可以获得视图的宽度高度..

    @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    int width = mTextView.getWidth();
    int height = mTextView.getHeight();
}

并定义TextView mTextView为全局

于 2013-10-28T11:28:27.450 回答
0

您必须使用 ViewTreeObserver 类,该类用于注册可以通知视图树中的全局更改的侦听器。这样的全局事件包括但不限于整个树的布局、绘制通道的开始、触摸模式的改变。

在 Activity 的 onCreate mehotd 中输入:

ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        int viewWidth = mTextView.getMeasuredWidth();

    }
});
于 2013-10-28T11:23:22.060 回答