在我的Android应用程序,我需要动态地创建几个LinearLayout中的文本。但我不能设置权重的每个元素。我想那LL看起来像XML部分:
<的LinearLayout 机器人:方向=横向 机器人:layout_width =FILL_PARENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_margin =10px的 机器人:weightSum =1 机器人:ID =@ + ID / QWE><的TextView 机器人:layout_weight =0.1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=一些/><的TextView 机器人:layout_weight =0.8 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文字=字/><的TextView 机器人:layout_weight =0.1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文字=这里 机器人:重力=右/>< / LinearLayout中>
这看起来不错,但我需要同样的动态。在java中code我写的:
的LinearLayout LL =新的LinearLayout(背景);LinearLayout.LayoutParams的LayoutParams =新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(Ⅱ);LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);params.setMargins(10,10,10,10);LinearLayout.LayoutParams params1 =参数;params1.weight = 0.15f;TextView中1 =新的TextView(背景);one.setLayoutParams(params1);one.setText(一些);ll.addView(之一);LinearLayout.LayoutParams params2 =参数;params2.weight = 0.7f;TextView的2 =新的TextView(背景);two.setLayoutParams(params2);two.setText(字);ll.addView(二);LinearLayout.LayoutParams params3 =参数;params3.weight = 0.15f;TextView中3 =新的TextView(背景);three.setLayoutParams(params3);three.setText(这里);ll.addView(三级);
但在这种情况下,我获得3 TextView中的等宽。看起来,我没加weightSum属性,主要LL,但我不知道该怎么做...
请帮帮忙!
感谢您!
解决方案在浮法preFER整数。这样你可以得到你想要的任何类型的分数(甚至1/3)。
如果您设置每个视图的重量,则不需要设置weightSum。
如果您设置的weightSum,你可以离开一个视图中没有任何重量,赋予其可用空间的其余部分。
好像你给所有的意见相同的LayoutParams,而不是克隆它为他们每个人的。当您使用params2 =参数; ,这意味着你设置它的一个引用,而不是您创建一个新的。在该方法的结束时,都将指向相同的LayoutParams,与0.15f的重量(因为这是最后一个)。
In my android app I need dynamically create few LinearLayout's with text. 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>
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);
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...
Please help!
Thank you!
解决方案prefer integer over float . this way you can get any kind of fraction you wish (even 1/3) .
if you set the weight of each of the views , you don't need to set the weightSum .
if you set the weightSum , you can leave one view without any weight , giving it the rest of the space available.
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) .