动态布局优先改变

时间:2014-07-16 13:45:13

标签: android android-layout

我必须创建一个包含5个网格视图(GV1,GV2,GV3,GV4,GV5)的布局。 gridviews的优先级分别为1,2,3,4,5。根据传递给适配器的arraylist的大小,计算gridview的权重

重量=尺寸*优先级

现在权重将决定布局中特定网格视图将占用的区域数量。 我已附上以下代码。但它不起作用。

   RelativeLayout rl =new RelativeLayout(this);
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT
                ,RelativeLayout.LayoutParams.MATCH_PARENT);

    GridView burger_GV = new GridView(this);
    burger_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (burgerAL.size()*BURGER_PRIORITY)));
    burger_GV.setBackgroundColor(Color.GRAY);

    GridView pizza_GV = new GridView(this);
    pizza_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (pizzaAL.size()*PIZZA_PRIORITY)));
    pizza_GV.setBackgroundColor(Color.RED);

    GridView sides_GV = new GridView(this);
    sides_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (sidesAL.size()*SIDES_PRIORITY)));
    sides_GV.setBackgroundColor(Color.YELLOW);


    GridView cookies_GV = new GridView(this);
    cookies_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (cookiesAL.size()*COOKIES_PRIORITY)));


    GridView beverages_GV = new GridView(this);
    beverages_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (beveragesAL.size()*BEVERAGES_PRIORITY)));


    burger_GV.setAdapter(new BurgerAdapter(burgerAL));
    pizza_GV.setAdapter(new BurgerAdapter(pizzaAL));
    sides_GV.setAdapter(new BurgerAdapter(sidesAL));
    cookies_GV.setAdapter(new BurgerAdapter(cookiesAL));
    beverages_GV.setAdapter(new BurgerAdapter(beveragesAL));

    rl.addView(burger_GV);
    rl.addView(pizza_GV);
    rl.addView(sides_GV);
    rl.addView(cookies_GV);
    rl.addView(beverages_GV);

    setContentView(rl,param);

任何人都可以向我推荐解决方案或任何想法。提前谢谢。

1 个答案:

答案 0 :(得分:1)

对所有GridView.LayoutParams

试试GridView
GV.setLayoutParams(new GridView.LayoutParams(0, 
            0, (burgerAL.size()*COOKIES_PRIORITY)));

它不起作用,因为您使用WRAP_CONTENT作为高度和宽度。当总和大于LinearLayout时,权重用于分配剩余的空白空间或占用空间。将宽度设置为0dip,它将起作用。 另外RelativeLayout不关注android:layout_weight。 (这是LinearLayout.LayoutParams的属性,但不属于RelativeLayout.LayoutParams