Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FrameLayout layout_weight programmatically

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?

like image 667
prostock Avatar asked Dec 20 '11 01:12

prostock


People also ask

How do you set linear layout weight programmatically?

LinearLayout. LayoutParams param = new LinearLayout. LayoutParams( 0, LayoutParams. MATCH_PARENT, 1);

What is FrameLayout in Java?

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.

How to give weight to LinearLayout in android?

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.


1 Answers

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);
like image 105
Nikola Despotoski Avatar answered Oct 02 '22 14:10

Nikola Despotoski