I have a vertical LinearLayout LL_p with two children horizontal LinearLayouts LL_1 and LL_2 which in turn have their own children. Based on the visible contents of LL_1 and LL_2, I want to dynamically change their relative weight inside LL_p. I already have an xml layout with a great deal of details that I do not wish to lose, so I only have to make the incremental change to the weights. How do I do that? Here is my xml
…
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=“0.2”
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_1”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:background="@drawable/some_image”
android:orientation="vertical" >
<!—- a number of includes —>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:layout_marginBottom="@dimen/dim_1”
android:background="@color/some_color”
android:orientation="horizontal" >
<!—- a number of children —>
</LinearLayout>
</LinearLayout>
</LinearLayout>
So in java activity class I figure I need the following method, but I need help completing it. Notice that I am not instantiating new layout params as that would cause me to lose my xml layout details; instead I am using getLayoutParams
to grab the one set from xml. so how do I make weight change to the layout as obtained?
private void adjustMYLayout(boolean flip) {
LayoutParams layout1 = mLL1.getLayoutParams();
LayoutParams layout2 = mBLL2.getLayoutParams();
//now what?
if(flip) {//set one weight system
}else {
//set other weight system
}
}
UPDATE for @nKn
private void adjustMYLayout(boolean flip) {
if (flip) {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
} else {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
}
}
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.
You can achieve that by declaring a LinearLayout.LayoutParams
object
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter (1f
) is the weight of the layout (in this case 1 (f
stands for float
)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With