onCreate() 方法完成前,是不会进行重新测量。这时候需要监听测量完毕后,对宽度进行获取
ViewTreeObserver vto = mSenderName.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mSenderName.getViewTreeObserver().removeGlobalOnLayoutListener(this); int textNameWidth = mSenderName.getWidth() + 10; mSenderMessage.setMinWidth(textNameWidth); Log.d("===", "textNameWidth" + textNameWidth); } });