简体   繁体   English

Android以编程方式创建视图的宽度

[英]Android get programmatically created view's width

I have TextView created programmatically, like that: 我以编程方式创建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();

But mTextView.getWidth() always returns 0 但是mTextView.getWidth()总是返回0

And if I try: 如果我尝试:

mTextView.getLayoutParams().width

It returns the LayoutParams corresponding value in the LayoutParams class (-1 or -2) 它返回LayoutParams类中的LayoutParams对应值(-1或-2)

How can I get the view's width ? 如何获得视图的宽度?

EDIT I need to do this here: 编辑我需要在这里这样做:

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

        }

EDIT SOLUTION I used this from @androiduser's answer: 编辑解决方案我从@ androiduser的答案中使用了这个:

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

Problem solved ! 问题解决了 !

I used this solution: 我用过这个解决方案:

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

get this way: 得到这样的方式:

tV.post(new Runnable() {
                    @Override
                    public void run() {
                        int width=tV.getMeasuredWidth();
                    }
                });

those values are constant value for FILL_PARENT and WRAP_CONTENT. 这些值是FILL_PARENT和WRAP_CONTENT的常量值。 If you want to check the view size you can try this way: 如果要查看视图大小,可以尝试这种方式:

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

you have to wait until android draws the TextView . 你必须等到android绘制TextView This way you are posting a Runnable in the tagMap queue, that is executed, hopefully after the textview is draw (so it should be weight and height) 这样你就可以在tagMap队列中发布一个Runnable,它被执行,希望在绘制textview之后(所以它应该是weight和height)

In this method you can get the width height of the view.. 在此方法中,您可以获得视图的宽度高度。

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

and define TextView mTextView as global 并将TextView mTextView定义为全局

You have to use ViewTreeObserver class which is used to register listeners that can be notified of global changes in the view tree. 您必须使用ViewTreeObserver类,该类用于注册可以在视图树中通知全局更改的侦听器。 Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change. 这样的全局事件包括但不限于整个树的布局,绘图通道的开始,触摸模式的改变。

In the Activity's onCreate mehotd put this: 在Activity的onCreate mehotd中把这个:

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

    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM