简体   繁体   中英

Add View Programmatically ignore padding

I am adding a view to my relative layout with has 16dp padding all around

View blackView = new View(RestaurantActivity.this);
                    blackView.setMinimumHeight(height);
                    blackView.setMinimumWidth(width);
                    blackView.setBackgroundColor(Color.BLACK);
                    blackView.setAlpha(0.5f);
                    rl.addView(blackView);

and this view is also has that padding, but I want it to fill the full screen and ignore the padding, how could this be done programmatically?

Thanks

EDIT

I tried this

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)blackView.getLayoutParams();
                    float scale = getResources().getDisplayMetrics().density;
                    int dpAsPixels = (int) (-16*scale + 0.5f);
                    params.rightMargin = dpAsPixels;
                    params.leftMargin = dpAsPixels;
                    params.bottomMargin = dpAsPixels;
                    blackView.setLayoutParams(params);

But got this error

 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.james.duss.ResturantActivity$1.onClick(ResturantActivity.java:156)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

Working code:

    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)blackView.getLayoutParams();
    float scale = getResources().getDisplayMetrics().density;
    int dpAsPixels = (int) (-16*scale + 0.5f);
    params.rightMargin = dpAsPixels;
    params.leftMargin = dpAsPixels;
    params.bottomMargin = dpAsPixels;
    blackView.setLayoutParams(params);

Set in your xml layout file at RelativeLayout : android:clipToPadding="false"

A better and cleaner solution to get padding in dp instead of pixel is the following.

You have to store the padding in resource files (ie dimens.xml ), so you can simply call:

int padding = getResources().getDimensionPixelOffset(R.dimen.padding);

It does the conversion for you. Then you can set the padding to your desired view, for example:

mPicImageView.setPadding(padding, padding, padding, padding);

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