Android-在后台填充UI视图

Android-在后台填充UI视图,android,view,background,Android,View,Background,我有一个ViewAnimator,其视图0是一个表布局,主要由可单击的图像视图行组成, 其视图1是一个欢迎屏幕。创建应用程序时,它不仅要填充图像视图 动态的,因为它必须知道每一个有多大,但它做了相当多的数字运算 那么,我想隐藏TableLayout,直到它准备好进行交互。然而,当我 视图1在onCreate中可见,应用程序稍后会抛出异常,因为它认为ImageView具有宽度 高度为零 有没有一种方法可以在另一个视图位于前景时动态初始化这样的视图, 如果不在XML中硬编码ImageView的大小,

我有一个ViewAnimator,其视图0是一个表布局,主要由可单击的图像视图行组成, 其视图1是一个欢迎屏幕。创建应用程序时,它不仅要填充图像视图 动态的,因为它必须知道每一个有多大,但它做了相当多的数字运算 那么,我想隐藏TableLayout,直到它准备好进行交互。然而,当我 视图1在onCreate中可见,应用程序稍后会抛出异常,因为它认为ImageView具有宽度 高度为零

有没有一种方法可以在另一个视图位于前景时动态初始化这样的视图, 如果不在XML中硬编码ImageView的大小,我就无法真正做到这一点,因为它取决于大小 屏幕的另一面

以下是主XML的相关部分:

<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TableLayout android:id="@+id/TheGridView" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"> 

            <ImageView android:id="@+id/TheImageView"
                android:tag="tag_TheImageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:background="#FF0"
                android:clickable="true"
                android:onClick="TheImageView_Clicked"
                android:layout_margin="2dip"                    
                android:contentDescription="@string/TheImageView"

请注意,将TableRow的布局高度更改为大于0的值没有效果-它仍然认为大小为0 x 0。

这是因为imageview的宽度和高度实际上为零,当onCreate执行时,没有任何可见或绘制的内容。您必须将该代码放在onWindowFocusChanged lifecycle方法中。

实际上,如果GridView中的TableLayout ID位于前台,则该代码有效。问题是,a出现了ImageView,但它们在XML中显示为设计的,b我不想显示UI,直到它准备好接受输入。为了让我通过在代码中填充背景来完成我要做的事情,TableView是否必须在某个时候可见?您是否尝试过使用V.setVisibilityView.INVISIBLE,然后将其放在前面?
TableLayout ScreenLayout = (TableLayout)findViewById(R.id.TheGridView);
ImageView V = (ImageView)ScreenLayout.findViewWithTag("tag_TheImageView");
int ButtonWidth = V.getWidth();
int ButtonHeight = V.getHeight();
Bitmap ButtonBitmap = Bitmap.createBitmap(ButtonWidth, ButtonHeight, Config.ARGB_8888);