Android:我可以/如何使用onPostCreate()方法获取一个视图的宽度并将其分配给另一个视图?

Android:我可以/如何使用onPostCreate()方法获取一个视图的宽度并将其分配给另一个视图?,android,android-layout,Android,Android Layout,我需要在运行时根据另一个视图的宽度设置视图的宽度 我想在onCreate()方法中设置宽度 textView1.setWidth(textView2.getWidth()); 由于视图的宽度尚未完全构造,因此在执行上述代码时,getWidth()返回零 我在onpostreate()方法中尝试了相同的代码,但问题仍然存在 下面的输出是0,0 protected void onPostCreate(BundlesavedInstanceState) { super.onPostCrea

我需要在运行时根据另一个视图的宽度设置视图的宽度

我想在
onCreate()
方法中设置宽度

 textView1.setWidth(textView2.getWidth());
由于视图的宽度尚未完全构造,因此在执行上述代码时,getWidth()返回零

我在
onpostreate()
方法中尝试了相同的代码,但问题仍然存在

下面的输出是
0,0

protected void onPostCreate(BundlesavedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    TextView textView = (TextView) findViewById(R.id.qwert);
    string = string + " , " + Integer.toString(textView.getWidth());
    Toast.makeText(this,string,Toast.LENGTH_LONG).show();
   }
我认为当所有视图都完全渲染(膨胀到各自的高度和宽度)时,将调用
onpostreate()
方法


为什么
onPostCreate()
方法的行为不符合预期

 View myView=findViewById(R.id.myView);
  myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //At this point the layout is complete and the 
                //dimensions of myView and any child views are known.
            }
        });

在获取视图宽度之前,需要添加一些延迟。 在onCreate()中这样做可以获得宽度


因为你所期望的不是真的。继续搜索感谢您的支持,但是onPostCreate()方法的工作是什么。onPostCreate表明onCreate()的执行刚刚完成。但是布局膨胀可能不会完成。@vivekkumarIs有没有像onPostCreate()这样的简单方法,只有在布局膨胀完成时才调用它?
  textView.post(new Runnable()
    {
        @Override
        public void run()
        {
            int viewWidth = textView.getWidth();
        }
    });