ImageView and LinearLayout, the imageview it's getting blank spaces on top and bottom of the Image

走远了吗. 提交于 2019-12-08 07:16:49

问题


I'm having some issues with LinearLayout and ImageView on Android. I must not use XML for defining the layouts, i'm trying to understand to do it with java code.

I have two elements on the linearLayout, a header (imageview) and a body (gridview with images). Someting is going wrong because the imageview it's getting some blanck spaces on top and on bottom of the original Image.

The original Imageview that i am using for the Header doesn't have these white spaces on top and on bottom of the image, i put the line header.setBackgroundColor(Color.WHITE); and then i noticed that something was going wrong with the imageview because its getting these blanck spaces.

I tested with multiple header images and i noticed that the higher height haves the image, the bigger are the blanck spaces so, they are generated dinamically for some kind of error/bug

How to remove them?

Here is a capture:

Here is the code:

LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    header = new ImageView(getApplicationContext());
    header.setImageResource(R.drawable.header_acc_old);
    header.setBackgroundColor(Color.WHITE);

    myGridView = new GridView(this);
    myImageAdapter=new ImageAdapter(this);
    myGridView.setAdapter(myImageAdapter);

    ll.addView(header);
    ll.addView(myGridView);
    setContentView(ll);

I Tryed with these solutions, but they didn't work:

ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(header,layoutParams);

回答1:


Try using setBackgroundResource instead of setImageResource while setting image to ImageView.



来源:https://stackoverflow.com/questions/7712435/imageview-and-linearlayout-the-imageview-its-getting-blank-spaces-on-top-and-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!