Android OnGlobalLayoutListener,这里发生了什么

时间:2015-08-11 16:37:59

标签: java android layout scrollview

背景

我一直在为我的工作做一个概念验证。要求是,首次启动应用程序时,会打开一个欢迎屏幕,向用户介绍应用程序和新功能。一切都很好,花花公子。欢迎屏幕是一个可滚动的长列表,其中突出显示了新功能。当这个可滚动列表首次打开时,屏幕中间将有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));
    }
});

问题:

通过代码我看到它正在设置每个&#34;部分&#34;的宽度和高度。是外部线性布局的宽度和高度。由于这是match_parent,因此每个部分的大小与屏幕大小相同。到目前为止有道理。现在,长滚动列表中有3个部分与屏幕的宽度和高度相匹配。要求说只有第一部分应与屏幕宽度和高度相匹配。因此,因为第一部分高度是match_parent而其他2部分高度是wrap_content。我认为删除这两行Java代码

f2.setLayoutParams(new LinearLayout.LayoutParams(width, height));
f3.setLayoutParams(new LinearLayout.LayoutParams(width, height));

应该使以下2个部分包含其内容。但它实际上导致第一部分显示为全屏,但应该滚动禁用,所以我无法向下滚动以查看其他部分。谁能解释一下这里发生了什么?如果有更好的方法来实现我在这里尝试做的事情,我很乐意听到它。

1 个答案:

答案 0 :(得分:0)

我实际上能够使用以下代码正常工作。原来这是一个问题,ImageView被设置为包装其内容,但由于没有图像只是颜色,它一定不知道在哪里包装。但是这段代码对我有用。

<强>布局

import csv
import sys

export = csv.DictReader(open("export.csv"),delimiter='\t')
sys.stdout = open('autobilling.txt','w')

def regOutput():
    for row in export:
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']
        print 'CL'
        print row['Order Store ID']

def pageChange():
    for row in export:
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']
        print ' '
        print 'P1'
        print 'M'
        print '5 CL'
        print row['Order Store ID']
        print ' '
        print 'A'

<强>爪哇

<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>