kggoh
kggoh

Reputation: 742

how to set only top margin of a layout

Hi I want to change my layout top margin, but remain other margin parameters unchanged, my source is inside a fragment, if i use the code below, my other parameters will be set to zero

       mainScreen = (RelativeLayout) v.findViewById(R.id.main_screen);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mainScreen.getLayoutParams();
       params.setMargins(0, 390, 0, 0); 
       mainScreen.setLayoutParams(params);

Thanks.

Upvotes: 0

Views: 494

Answers (2)

Harsh Parikh
Harsh Parikh

Reputation: 3845

You can use this just to set only top margin :

params.topMargin=10; //It will set value in pixel

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157467

just read the other margin from the LayoutParams object you get form getLayoutParams()

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mainScreen.getLayoutParams();
params.setMargins(params.leftMargin, 390, params.rightMargin, params.bottomMargin); 

Upvotes: 2

Related Questions