java - ImageView setBackground 未正常运行

标签 java android background imageview

我有一个使我成为图像按钮的类(class)。这个图像按钮包含一个 ImageView 、两个 TextView 和一个按钮。

我在 ImageView 中放置了一些宽度,如果我将一个图像放入 xml 中作为背景,它可以正常工作,但是如果我用 java 代码这样做,它就不起作用,它就像 src 属性一样。

(图像显示为没有宽度,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>

最佳答案

尝试使用setImageDrawable(Drawable)相反 setBackground(Drawable)

或者你可以直接使用setImageResource(int)带资源id

关于java - ImageView setBackground 未正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634502/

相关文章:

android - ChildFragmentManager.findFragmentByID() 在 android 4.4.2 上返回 null

具有背景颜色和波纹效果的 Android 按钮

ios - 如何在应用程序处于后台时同步位置

java - 如何选择同步对象还是方法

java - OpenCV - 二进制垫上的线条细化会导致随机水平虚线

java - 有没有办法生成仅由数字组成的随机 UUID?

android - 将图像共享到其他应用程序

java - 如何在循环中迭代多个不同类型的数组?

Android Volley - 带有 post 参数的 JsonArrayRequest

Android 停止背景音乐