The cause of

The story starts with a small feature. A recent requirement was to make a long-press copy title function. This should have been a simple requirement. Simply set the Android :textIsSelectable=”true” to the textView. But it is the son to give brother.

View popView = LayoutInflater.from(mContext).inflate(R.layout.copy_background, null, false);
        mCopyPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mCopyPopupWindow.setOutsideTouchable(true);
        mCopyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        popView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SystemUtil.copyToClipboard(data); mCopyPopupWindow.dismiss(); }}); mCopyPopupWindow.showAsDropDown(view, view.getWidth() / 2 - popView.getWindth()/2, -popView.getWidth()- view.getHeight());Copy the code

Make it appear as the system does. Well, so easy. When I finished writing the code and ran through it, I suddenly realized that the location was wrong. As is shown in

PopView. Measure (0, 0);

after

Look at the source code. So I dug up the code in the source code

/**
     * Return the width of the your view.
     *
     * @return The width of your view, in pixels.
     */
    @ViewDebug.ExportedProperty(category = "layout")
    public final int getWidth() {
        return mRight - mLeft;
    }

Copy the code
 /**
     * Like {@link #getMeasuredWidthAndState()}, but only returns the
     * raw width component (that is the result is masked by
     * {@link #MEASURED_SIZE_MASK}).
     *
     * @return The raw measured width of this view.
     */
    public final int getMeasuredWidth() {
        return mMeasuredWidth & MEASURED_SIZE_MASK;
    }
    
    
     /**
     * Width as measured during measure pass.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "measurement")
    int mMeasuredWidth;

Copy the code

We know this from the comments and comments on both ends of the code. The measureWidth obtained by getMeasureWidth () corresponds to the View being measured, while the width obtained by getWidth () corresponds to the View being Layout. The Measure (0,0) method we call before the getMeasureWidth () method makes the view go through the Measure process. At this point, there is a general understanding of the difference between the two methods.

The results of

So what good are we if we know that? I thought about it for a while, and I think I’ve got a solution to a problem. Is in the Activity of onCreate, onStart and other methods how to get a view of the length and width, BELIEVE that smart you should also know how to play it.

Nonsense a bit much, you see the officer lightly spray. There is a lot of wrong advice