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.