I'm trying to create a FrameLayout
programmatically as follows(this view will float on the the bottom center of a LinearLayout
):
FrameLayout bottomFrameLayout = new FrameLayout(context);
bottomFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(0,LayoutParams.FILL_PARENT,Gravity.CENTER | Gravity.BOTTOM));
Since I set layout_width=0dp
, I would like to also set layout_weight
to control the width. How can I do that?
LinearLayout. LayoutParams param = new LinearLayout. LayoutParams( 0, LayoutParams. MATCH_PARENT, 1);
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
Weight can only be used in LinearLayout . If the orientation of linearlayout is Vertical, then use android:layout_height="0dp" and if the orientation is horizontal, then use android:layout_width = "0dp" . It'll work perfectly.
You need to use LinearLayout.LayoutParams (int width, int height, float weight)
constructor since FrameLayout
is child of LinearLayout
therefore you are setting params for child in LinearLayout
.
Edit:
FrameLayout bottomFrameLayout = new FrameLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT,mWeight);
lp.gravity = Gravity.CENTER | Gravity.BOTTOM;
bottomFrameLayout.setLayoutParams(lp);
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