Android oncreate()方法后linearlayout的高度错误,为什么?

Android oncreate()方法后linearlayout的高度错误,为什么?,android,android-layout,Android,Android Layout,我想通过getHeight()获取linearlayout的高度,但我发现高度错误,因此我做了一些检查错误: 第一个:oncreate()方法之后的int height=linearLayout.getheight() 然后线性布局。设置填充(0,-高度,0,0) 但结果是线性布局的内容并没有完全隐藏。 为什么? 计划\详细信息\课程\布局\摘要.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

我想通过getHeight()获取linearlayout的高度,但我发现高度错误,因此我做了一些检查错误: 第一个:oncreate()方法之后的int height=linearLayout.getheight()

然后线性布局。设置填充(0,-高度,0,0)

但结果是线性布局的内容并没有完全隐藏。 为什么?

计划\详细信息\课程\布局\摘要.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f4f4f4"
android:orientation="vertical" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="课程简介"
    android:textColor="#034187"
    android:textSize="20sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/course_detail_intro_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#929496"
        android:textSize="14sp" />

    <Button
        android:id="@+id/course_detail_intro_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:background="@drawable/bg_btn"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="更多"
        android:textColor="#fff"
        android:textSize="10sp" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/course_intro_border" />

</LinearLayout>

到onCreate结束时,布局尚未完成。您可以注册布局侦听器,以便在布局完成且(正确)高度可用时收到通知。例如:

layout_course_summary.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int height = layout_course_summary.getHeight();

        ...

        layout_course_summary.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

这是正确的方法:

LinearLayout layout_course_summary = (LinearLayout)findViewById(R.id.plan_detail_course_layout_summary);

ViewTreeObserver observer= layout_course_summary.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                                               int height=layout_course_summary.getHeight();
                                               layout_course_summary.setPadding(0, -height, 0, 0);
                    }
                });
调用需要API级别16(当前最小值为14):android.view.ViewTreeObserver#removeOnGlobalLayoutListener,但我希望兼容API 14我相信API 16已被替换。RemoVegLobalOnlyOutliner仍应在API 14中工作。
LinearLayout layout_course_summary = (LinearLayout)findViewById(R.id.plan_detail_course_layout_summary);

ViewTreeObserver observer= layout_course_summary.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                                               int height=layout_course_summary.getHeight();
                                               layout_course_summary.setPadding(0, -height, 0, 0);
                    }
                });