安卓:设置权重参数编程做了什么,我想的相反 [英] Android: Setting the Weight parameter programmatically does the opposite of what i want

查看:119
本文介绍了安卓:设置权重参数编程做了什么,我想的相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Android的一个问题。 我设置在Java中的权重参数,但它究竟在做什么,我想要的oposite。

i'm having a problem with android. I'm setting the Weight parameter in Java, but it's doing exactly the oposite of what i want.

这里的code

LinearLayout container = new LinearLayout(context);
// some code ...
container.setWeightSum(1f);

View v1 = new View(context);
v1.setBackgroundColor(Color.parseColor("#ff0000"));
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p1.weight=0.1f;

View v2 = new View(context);
v2.setBackgroundColor(Color.parseColor("#000000"));
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p2.weight=0.9f;

container.addView(v1,p1);
container.addView(v2,p2);

我重复这个过程7次,将容器布局之间的黑线。 通常情况下,我应该得到一个小红柱在LEF,和大黑的,但这里是我这个code得到:

I repeat this process 7 times with adding a black line between the container layout. Normally i should get a small red column on the lef, and a large black one, but here's what i get with this code :

它为什么这样做完全的code相反?

Why does it doing exactly the opposite of the code ?

感谢您的帮助球员。

推荐答案

当我们使用wieght宽度应该是零

When we use the wieght width should be Zero

尝试用宽度为0的容器内儿童.............

try with width 0 for with children inside the container.............

 LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);



LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);

这篇关于安卓:设置权重参数编程做了什么,我想的相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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