У меня есть класс, который делает меня образной кнопкой. Эта кнопка изображения содержит изображение, текст буксирного текста и кнопку.Вид изображения setBackground не действует должным образом
Я поместил некоторую ширину в вид изображения, и если я поместил в 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>
Та же проблема .. Я попробую, но изображение не адаптируется к размеру фона. –
Предоставляете ли вы ресурс drawabale следующим образом? getResources.getDrawable (R.drawable.yourImage); – Owehbeh