繁体   English   中英

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

[英]Android get programmatically created view's width

我以编程方式创建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();

问题解决了 !

我用过这个解决方案:

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

得到这样的方式:

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

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

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

你必须等到android绘制TextView 这样你就可以在tagMap队列中发布一个Runnable,它被执行,希望在绘制textview之后(所以它应该是weight和height)

在此方法中,您可以获得视图的宽度高度。

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

并将TextView mTextView定义为全局

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

在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