如何为主LL添加weightSum属性? [英] How to add weightSum attribute for main LL?

查看:102
本文介绍了如何为主LL添加weightSum属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我需要动态创建一些带有文本的LinearLayout.

In my Android app I need dynamically create few LinearLayout's with text.

但是我不能设置每个元素的权重.我希望LL在XML部分中看起来像这样:

But I can not set weight of each element. I want that LL looks like in XML part:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10px"
    android:weightSum="1"
    android:id="@+id/qwe">
<TextView 
    android:layout_weight="0.1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="some"
/>
<TextView 
    android:layout_weight="0.8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="word"
/>
<TextView 
    android:layout_weight="0.1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="here"
    android:gravity="right"
/>
</LinearLayout>

看起来不错,但我需要动态地添加. 在Java代码中,我写道:

It looks good but I need the same dynamically. In Java code I wrote:

LinearLayout ll = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
ll.setLayoutParams(layoutParams);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(0xFF888888);
rootLL.addView(ll);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);

LinearLayout.LayoutParams params1 = params;
params1.weight = 0.15f;
TextView one = new TextView(context);
one.setLayoutParams(params1);
one.setText("some");
ll.addView(one);

LinearLayout.LayoutParams params2 = params;
params2.weight = 0.7f;
TextView two = new TextView(context);
two.setLayoutParams(params2);
two.setText("word");
ll.addView(two);

LinearLayout.LayoutParams params3 = params;
params3.weight = 0.15f;
TextView three = new TextView(context);
three.setLayoutParams(params3);
three.setText("here");
ll.addView(three);

但是在这种情况下,我获得了三个宽度相等的textView.看来我没有为主LL添加weightSum属性,但我不知道该怎么做.

But in this case I obtain three textView's with equal width. It looks that I did not add weightSum attribute for main LL but I don't know how to do it.

推荐答案

  1. 优先使用整型而不是float.这样,您可以得到任意分数(甚至是1/3).

  1. prefer integer over float . this way you can get any kind of fraction you wish (even 1/3) .

如果您设置每个视图的权重,则无需设置weightSum.

if you set the weight of each of the views , you don't need to set the weightSum .

如果设置weightSum,则可以保留一个视图而没有任何权重,从而为其提供剩余的可用空间.

if you set the weightSum , you can leave one view without any weight , giving it the rest of the space available.

似乎您为所有视图提供了相同的layoutparams,而不是为每个视图都克隆了它们.当您使用"params2 = params;"时,则意味着您设置了对它的引用,而不是创建一个新的引用.在该方法的最后,所有都将指向相同的layoutParams,权重为0.15f(因为这是最后一个).

it seems you give all of the views the same layoutparams instead of cloning it for each of them . when you use "params2 = params;" , it means that you set a reference to it and not that you create a new one . in the end of the method , all will point to the same layoutParams , with the weight of 0.15f (since that's the last one) .

这篇关于如何为主LL添加weightSum属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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