繁体   English   中英

影像检视setBackground运作不正常

[英]Image view setBackground is not acting properly

我有一堂课,使我成为一个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>

尝试使用setImageDrawable(Drawable)代替setBackground(Drawable)

或者您可以直接将setImageResource(int)与资源ID一起使用

使用此方法:

ImageProduct.setImageDrawable(new_image);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM