Android的动态下相互补充布局 [英] android dynamically add layouts under each other

查看:138
本文介绍了Android的动态下相互补充布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态下相互补充多种布局。所以我写了下面code:

I'm trying to add multiple layouts dynamically under each other. So I wrote the following code:

   for (int i = 1; i <= layoutCounter; i++) {
        View neu = inflater.inflate(R.layout.vote, parent, false);
        neu.setId(layoutID);

        if (layoutID == 1) {
            params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, R.id.txtMultiline);

        } else {

            params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, neu.getId());

        }
        neu.setLayoutParams(params);
        parent.addView(neu);

        layoutID++;
    }

txtMultiline是一个固定视图中的XML定义。 LayoutID是整数,从1开始的第一布局是根据txtMultiline TextView的正确添加。但是,所有下列布局刚上车的父布局(这是一个RelativeLayout的)的顶部加入。无法获取..其他路由是正确执行的原因。但是,下面的常量,似乎试图将它应用到我的动态充气布局时没有任何效果。我究竟做错了什么?

txtMultiline is a fixed View defined in XML. LayoutID is an integer and starts with 1. The first layout is added correctly under the txtMultiline TextView. But all following layouts just get added on top of the parent layout (which is a RelativeLayout). Can't get the reason.. else-route is executed correctly. But the BELOW constant seems to have no effect when trying to apply it to my dynamically inflated layouts. What am I doing wrong?

推荐答案

也许有:

params.addRule(RelativeLayout.BELOW,layoutID-1);

来代替:

params.addRule(RelativeLayout.BELOW,neu.getId());

在你的其他条件。

这篇关于Android的动态下相互补充布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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