简体   繁体   中英

Set the Fragment Layout Margin Programmatically at run time

I want to set the Layout margin of fragment programmatically. Is there way to do this.

As I tried with MapView like this.

LayoutParams layoutParams = (RelativeLayout.LayoutParams) mapView.getLayoutParams();
layoutParams.setMargins(0, 0, 0, 0);
mapView.setLayoutParams(layoutParams);

Can it be achieved same in case of fragment.

In the onCreateView method of the fragment you could use the following code to set the view programmatically:

// Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.sample_fragment, container, false);
    LayoutParams layoutParams=new LayoutParams(width, height);
    layoutParams.setMargins(leftMargin, topMargin, 0, 0);
    view.setLayoutParams(layoutParams);

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