简体   繁体   中英

layout_below and weightsum of LinearLayout Programmatically

I am creating LinearLayout in RelativeLayout programmatically. What I am trying to do is to assign the weigtsum in LinearLayout and want to set the layout_above attribute as well. The problem is weightSum is available in LinearLayout.LayoutParams and layout_above is available in RelativeLayout.LayoutParams . Currently,, I am doing

LinearLayout ll_menu_code = new LinearLayout(this);
        ll_menu_code.setId(1001);
        LinearLayout.LayoutParams parmas_ll_menu_code = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
        parmas_ll_menu_code.setMargins(0, 20, 0, 0);
        ll_menu_code.setLayoutParams(parmas_ll_menu_code);

Which sets the weightSum . How to set the layout_above ? What should the way to set the both attributes?

I want to create the following xml layout programmatically

<LinearLayout
                                android:layout_below="@id/ll_menu_code"
                                android:id="@+id/ll_quantity"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginTop="20dp"
                                android:weightSum="5">

If you want to add LinearLayout into RelativeLayout programmatically, then you must create instance of RelativeLayout.LayoutParams rather than LinearLayout.LayoutParams since LinearLayout is a child to Relativelayout

So your code must be

LinearLayout ll_menu_code = new LinearLayout(this);
    ll_menu_code.setId(1001);
    ll_menu_code.setWeightSum(2f);        

    RelativeLayout.LayoutParams parmas_ll_menu_code = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    parmas_ll_menu_code.setMargins(0, 20, 0, 0);
    parmas_ll_menu_code.addRule(RelativeLayout.BELOW, 1001);

    ll_menu_code.setLayoutParams(parmas_ll_menu_code);

I Hope it helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM