Java 安卓OnGlobalLayoutListener,这里发生了什么

Java 安卓OnGlobalLayoutListener,这里发生了什么,java,android,layout,scrollview,Java,Android,Layout,Scrollview,背景: 我一直在为我的工作进行概念验证。要求是,当应用程序首次启动时,会打开欢迎屏幕,向用户介绍应用程序和新功能。那里一切都很好。欢迎屏幕是一个长长的滚动列表,其中列出了突出显示新功能的部分。当这个可滚动列表首先打开时,屏幕中间有2个文本部分,屏幕底部有一个文本,让用户知道向下滚动更多。之后,每个部分基本上应该只包装其内容。我找到了一个让它工作的方法,但我很难弄清楚它是如何工作的,为什么工作的。下面是有效的代码 布局: <ScrollView xmlns:android="http://s

背景:

我一直在为我的工作进行概念验证。要求是,当应用程序首次启动时,会打开欢迎屏幕,向用户介绍应用程序和新功能。那里一切都很好。欢迎屏幕是一个长长的滚动列表,其中列出了突出显示新功能的部分。当这个可滚动列表首先打开时,屏幕中间有2个文本部分,屏幕底部有一个文本,让用户知道向下滚动更多。之后,每个部分基本上应该只包装其内容。我找到了一个让它工作的方法,但我很难弄清楚它是如何工作的,为什么工作的。下面是有效的代码

布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- first section -->
    <LinearLayout
        android:id="@+id/f1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="top text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:paddingTop="35dp"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="middle text"
            android:layout_gravity="center_horizontal"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="bottom text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:textColor="@android:color/holo_red_dark"/>
    </LinearLayout>

    <!-- second section -->
    <ImageView
        android:id="@+id/f2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff8222"/>

    <!-- third section -->
    <ImageView
        android:id="@+id/f3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"/>
</LinearLayout>
final LinearLayout layout = (LinearLayout)findViewById(R.id.linlayout);
final LinearLayout f1=(LinearLayout) findViewById(R.id.f1);
final ImageView f2=(ImageView) findViewById(R.id.f2);
final ImageView f3=(ImageView) findViewById(R.id.f3);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int width = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight();
        f1.setLayoutParams(new LinearLayout.LayoutParams(width, height));
        f2.setLayoutParams(new LinearLayout.LayoutParams(width, height));
        f3.setLayoutParams(new LinearLayout.LayoutParams(width, height));
    }
});
问题:

通过代码,我看到它将每个“部分”的宽度和高度设置为外部线性布局的宽度和高度。由于这是匹配父项,所以每个部分的大小与屏幕的大小大致相同。到目前为止是有道理的。现在,长滚动列表中有3个部分与屏幕的宽度和高度相匹配。要求说只有第一部分应该与屏幕的宽度和高度相匹配。因此,由于第一个部分的高度是match_parent,另外两个部分的高度是wrap_content。我想删除这两行Java代码

f2.setLayoutParams(新的LinearLayout.LayoutParams(宽度、高度))
f3.setLayoutParams(新的LinearLayout.LayoutParams(宽度、高度))


应使以下两个部分包装其内容。但是它实际上会导致第一部分全屏显示,就像它应该显示的那样,但是滚动被禁用,所以我无法向下滚动查看其他部分。有人能解释一下这是怎么回事吗?如果有更好的方法来实现我在这里要做的事情,我很想听听。

我实际上能够通过下面的代码让它正常工作。事实证明,ImageView被设置为包装其内容是一个问题,但由于并没有图像只是颜色,它一定不知道在哪里包装。但是这个代码对我有用

布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/f1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="top text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:paddingTop="35dp"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="middle text"
            android:layout_gravity="center_horizontal"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="bottom text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:textColor="@android:color/holo_red_dark"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/f2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="middle text"
            android:layout_gravity="center_horizontal"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:textColor="@android:color/holo_red_dark"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/f3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="middle text"
            android:layout_gravity="center_horizontal"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom text"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom"
            android:textColor="@android:color/holo_red_dark"/>
    </LinearLayout>
</LinearLayout>
final LinearLayout layout = (LinearLayout)findViewById(R.id.linlayout);
    final LinearLayout f1=(LinearLayout) findViewById(R.id.f1);
    ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            int width = layout.getMeasuredWidth();
            int height = layout.getMeasuredHeight();

            f1.setLayoutParams(new LinearLayout.LayoutParams(width, height));
        }
    });