Java 图像视图设置背景未正常工作

Java 图像视图设置背景未正常工作,java,android,background,imageview,Java,Android,Background,Imageview,我有一个类使我成为一个imagebutton。此图像按钮包含一个图像视图、两个文本视图和一个按钮 我在图像视图中放置了一些宽度,如果我在xml中放置一个图像到背景,它可以正常工作,但是如果我使用java代码这样做,它就不工作了,它就像src atribute一样 (图像被显示为没有宽度,src和backgorund都没有宽度) 这是我的Java代码: public class MiBoton extends RelativeLayout{ protected int idImagen; pro

我有一个类使我成为一个imagebutton。此图像按钮包含一个图像视图、两个文本视图和一个按钮

我在图像视图中放置了一些宽度,如果我在xml中放置一个图像到背景,它可以正常工作,但是如果我使用java代码这样做,它就不工作了,它就像src atribute一样

(图像被显示为没有宽度,src和backgorund都没有宽度)

这是我的Java代码:

public class MiBoton extends RelativeLayout{

protected int idImagen;
protected String nomProducte;
protected double preuProducte;
protected String sPreuProducte;
//protected MainActivity maPrincipal;

public MiBoton(Context context, int imgImageResource, String sName, double iPrice){

    super(context);
    this.idImagen = imgImageResource;
    this.nomProducte = sName;
    this.preuProducte = iPrice;
    this.sPreuProducte = Double.toString(iPrice) + " €";


    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.products, this, true);

    ImageView ImageProduct = (ImageView)findViewById(R.id.imatgeproducte);
    TextView ProductName = (TextView)findViewById(R.id.nomproducte);
    ProductName.setTextSize(10);

    TextView ProductPrice = (TextView)findViewById(R.id.preuproducte);


    Drawable new_image= getResources().getDrawable(imgImageResource);
    ImageProduct.setBackground(new_image);

   // ImageProduct.setBackgroundResource(imgImageResource);
    ProductName.setText(sName);
    ProductPrice.setText(sPreuProducte);


    Button btnPedir = (Button)findViewById(R.id.btnPedir);


}


public int getIdImagen() { return idImagen; }
public String getNomProducte() { return nomProducte; }
public double getPreuProducte() { return preuProducte; }

}
这是我的XML代码:

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

<ImageView
    android:id="@+id/imatgeproducte"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:clickable="true"
    />
<TextView
    android:id="@+id/nomproducte"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imatgeproducte"
    android:textSize="14sp"
    />
<TextView
    android:id="@+id/preuproducte"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nomproducte"
    android:textSize="14sp"
    />
<Button
    style="@style/btnStyleGenoa"
    android:layout_width="wrap_content"
    android:layout_height="32dp"
    android:text="Demanar"
    android:textSize="10dp"
    android:id="@+id/btnPedir"
    android:layout_below="@+id/preuproducte"
    />
</RelativeLayout>

尝试改用

或者您可以直接与资源id一起使用

使用此方法:

ImageProduct.setImageDrawable(new_image);

我也有同样的问题,图像以其原始大小显示,与背景大小不匹配。是否尝试将ImageView的缩放类型设置为centerInside?同样的问题。。我试过了,但是图像不适合背景大小。您是否提供了如下的Drawabable资源?getResources.getDrawable(R.drawable.yourImage);