Android 为什么onCreate中的视图高度为零
我试图将视图高度作为onCreate内动画的最大值传递给自定义手势侦听器:Android 为什么onCreate中的视图高度为零,android,view,Android,View,我试图将视图高度作为onCreate内动画的最大值传递给自定义手势侦听器: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mParent = findViewById(R.id.root_ll);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mParent = findViewById(R.id.root_ll);
mImage1 = findViewById(R.id.image1);
mImage1.setOnTouchListener(this);
float max = mParent.getHeight() - mImage1.getHeight();
mGestureDetector = new GestureDetectorCompat(this, new CustomGestureListener(mImage1, 0, max));
}
问题是0作为max传递,这表明在onCreate方法中mRoot作为no height传递。然而,当我在另一个方法(比如onTouch)中得到它的高度时,它就有了高度。mRoot的布局如下所示:
<LinearLayout
android:id="@+id/root_ll"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#4CAF50"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:src="@drawable/myimage" />
</LinearLayout>
这是因为在呈现视图之前调用了“”方法。在您的例子中,布局没有渲染,因此容器还没有高度
我认为应该在“”处完成渲染。谢谢。尝试了这个,仍然高度是零内恢复以及。