简体   繁体   中英

How to set Margin for Relative Layout programmatically in android?

I am facing an issue to set Margin for Relative Layout. I have tried so many ways, but nothing helps me.

-Try#1

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,      
                            LayoutParams.WRAP_CONTENT);
 params.setMargins(0, 10, 0, 0);
 _rlMain.setLayoutParams(params);

-Try#2

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
params.setMargins(0, 50, 0, 0); 
_rlMain.setLayoutParams(params);

-Try#3

RelativeLayout.LayoutParams par=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
par.setMargins(0, 50, 0, 0);
_rlMain.setLayoutParams(par);

-Try#4

FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rlMain.getWidth(), _rlMain.getHeight());
_rootLayoutParams.setMargins(0, 10, 0, 0);
_rlMain.setLayoutParams(_rootLayoutParams);

Here is my XML part:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/main_camera_act_rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

          </RelativeLayout>

</RelativeLayout>

if you are using RelativeLayout inside LinearLayout, you need to use LinearLayout.LayoutParams instead of RelativeLayout.LayoutParams.

So in above case replace your following code...

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
            params.setMargins(0, 50, 0, 0); 
            _rlMain.setLayoutParams(params);

with...

LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(0, 50, 0, 0);
relativeLayout.setLayoutParams(relativeParams);
relativeLayout.requestLayout();

Your width and height is set to "MatchParent" which will not take margin into considerations. Try "WrapContent" to height or width then try your "-Try#2"

Try this hope it will help you,its working for me

RelativeLayout relativeLayout = new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
        relativeParams.setMargins(20, 20, 20, 20);
        relativeLayout.setLayoutParams(relativeParams);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));

try this:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlMain.getLayoutParams();
            params.setMargins(50,50,50,50); 
            _rlMain.setLayoutParams(params);

This is your XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:id="@+id/rl"
        android:background="@android:color/black"
        android:layout_height="match_parent" >
</RelativeLayout>

This is you Java file :

RelativeLayout rl, rlCustom;

rl = (RelativeLayout) findViewById(R.id.rl);
rlCustom = new RelativeLayout(MainActivity.this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
params.setMargins(50, 50, 50, 50);
rlCustom.setLayoutParams(params);
rlCustom.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
rl.addView(rlCustom);

There you go!!!

android.view.ViewGroup.LayoutParams layoutParams = image.getLayoutParams();

    WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);

    Display display = wm.getDefaultDisplay();
    DisplayMetrics displaymatrics = new DisplayMetrics();
    display.getMetrics(displaymatrics);

    int displayWidth=0;
    try{
        Point size = new Point();
        display.getSize(size);
        displayWidth=size.x;
        //displayWidth = displaymatrics.widthPixels;
    }catch(Exception e)
    {
        e.printStackTrace()
    }
    ((RelativeLayout.LayoutParams) layoutParams).setMargins(10, 10, 10, 10);
    image.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