Mike_ShangJun
Mike_ShangJun

Reputation: 13

Why setBackground with Drawable can not be completely filled

Actual display, its height should be three rows, but only two rows?

myView = new ImageView(mContext) //or new TextView();
int height = 128 * 3;
int width = 128;
RelativeLayout.LayoutParams layoutParams;
layoutParams = new RelativeLayout.LayoutParams(width, height);
layoutParams.setMargins(xxx, xxx);
myView.setBackground(xxx);

enter image description here

Upvotes: 1

Views: 67

Answers (2)

Mukesh M
Mukesh M

Reputation: 2290

Try:

layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

myView.setScaleType(ImageView.ScaleType.FIT_XY);

Full Code:

ImageView myView = new ImageView(this);
        //int height = 128 * 3;
        //int width = 128;
        RelativeLayout.LayoutParams layoutParams;
        layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(1, 1, 1, 1);
        myView.setScaleType(ImageView.ScaleType.FIT_XY);
        myView.setBackground(getDrawable(R.drawable.art_clouds));


        RelativeLayout relativeLayout=new RelativeLayout(this);
        relativeLayout.setLayoutParams(layoutParams);
        relativeLayout.addView(myView);
        setContentView(relativeLayout);

Also See:

https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Upvotes: 0

Ara Hakobyan
Ara Hakobyan

Reputation: 1692

You're using pixels ,,, try this

final float scale = getResources().getDisplayMetrics().density;
int height = (int) 128 * 3 * scale;
int width = (int) 128 * scale;

Upvotes: 1

Related Questions