Android的网格布局只显示最后一个孩子 [英] Android GridLayout only shows last child

查看:330
本文介绍了Android的网格布局只显示最后一个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新Android开发。我一直在与使用网格布局,以显示动态插入ImageViews。

I am new to Android Development. I have been working with using a GridLayout to display Dynamically inserted ImageViews.

我的问题是位于onFocusWindowChanged,但我贴我的onCreate,我做我的图像的分配。

My issue is located in "onFocusWindowChanged" but I pasted my onCreate where I do my assignments of the images.

private List<Behavior> behaviors = null;
private static int NUM_OF_COLUMNS = 2;
private List<ImageView> images;
private GridLayout grid;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_behaviors);

    XMLPullParserHandler parser = new XMLPullParserHandler();

    try {
        behaviors = parser.parse(getAssets().open("catagories.xml"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    grid = (GridLayout) findViewById(R.id.behaviorGrid);
    images = new ArrayList<ImageView>();

    grid.setColumnCount(NUM_OF_COLUMNS);
    grid.setRowCount(behaviors.size() / NUM_OF_COLUMNS);

    for (Behavior behavior : behaviors)
        images.add(this.getImageViewFromName(behavior.getName()));

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);
    View view = (View) findViewById(R.id.scrollView);

    int width = (int) (view.getWidth() * .45);
    Log.i("ViewWidth", Integer.toString(width));

    GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
    lp.height = width;
    lp.width = width;

    int childCount = images.size();

    ImageView image;

    for (int i = 0; i < childCount-1; i++) {

        image = images.get(i);
        image.setLayoutParams(lp);      
        grid.addView(image);

    }

}

在我的(短)previous经验,采用

In my (short) previous experience, using

grid.add(View); 

工作正常,但现在我只看到只有最后一个子画面。通过调试找我可以看到在GridView被填充的不仅仅是最后一个元素,以及最后的ImageView的多。

worked fine, but now I am only seeing the last child display only. Looking through the debugger I can see that the gridview is being populated with more than just the last element, as well as the last imageview.

感谢您的帮助。

推荐答案

您应该为每个ImageView的一个GridLayout.LayoutParams:

you should create a GridLayout.LayoutParams for each ImageView:

for (int i = 0; i < childCount-1; i++) {
    GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
    lp.height = width;
    lp.width = width;

    ......
}

GridLayout.LayoutParams包含的位置信息,例如[柱:2,行:3]。在code,所有ImageViews都设置在同一GridLayout.LayoutParams,所以它们存在于相同的细胞(相互重叠)。

GridLayout.LayoutParams contains location information, e.g [column:2, row:3]. In your code, all ImageViews are set the same GridLayout.LayoutParams, so they are located in the same cell(overlapping each other).

在使用LinearLayout.LayoutParams相反,在它里面没有位置信息。网格布局会为每个子视图新GridLayout.LayoutParams,因此所有ImageViews用自己不同的GridLayout.LayoutParams和位置。

When use LinearLayout.LayoutParams instead, there is no location information in it. GridLayout will create a new GridLayout.LayoutParams for each child view, so all ImageViews use their own different GridLayout.LayoutParams and location.

预祝帮助。您可以阅读GridLayout.java和ViewGroup.java的更多细节。

Wish this help. You can read the GridLayout.java and ViewGroup.java for more details.

这篇关于Android的网格布局只显示最后一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆