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

Pum*_*kin 20 android textview

我以编程方式创建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();
Run Code Online (Sandbox Code Playgroud)

但是mTextView.getWidth()始终返回0

如果我尝试:

mTextView.getLayoutParams().width
Run Code Online (Sandbox Code Playgroud)

它返回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);

        }
Run Code Online (Sandbox Code Playgroud)

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

mTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                int width = mTextView.getMeasuredWidth();
                int height = mTextView.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)

问题解决了 !

Sid*_*yas 15

我用过这个解决方案:

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 :)
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 检查一下,可能是你遗漏了一些东西:http://www.sherif.mobi/2013/01/how-to-get-widthheight-of-view.html (2认同)