2012-05-14 31 views
7

böyle xml dosyasında düğmesi oluşturmak sol:Düğme ve çekilebilir

<Button 
     android:id="@+id/call_button" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="30dp" 
     android:background="@drawable/button" 
     android:layout_weight="40" 
     android:drawableLeft="@drawable/symbol_phone" 
     android:paddingLeft="20dp" 
     android:drawablePadding="-25dp" 
     android:text="Call" 
     android:textColor="@color/white" 
     /> 
ben aktivitesinde drawableLeft nasıl yapabileceğimizi bilmek istiyorum

. Bunun aptalca olduğunu biliyorum ama bunu etkinlikte yapmam gerekiyor çünkü orada buton oluyorum. Etkinliğimde xml dosyasında sahip olduğum şeyi nasıl yapabilirim? Eklenebilir çekilip çıkarılabilir dolgu ve dolgu malzemesi ihtiyacım var. Bu benim aktivite

Button button1 = new Button(this); 
button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth, buttonHeight)); 
button1.setText(systemTexts.getShowCallButton()); 
button1.setBackgroundDrawable(new          
button1.setTextColor(Color.parseColor(buttonTextColor)); 

cevap

20
Drawable image = getContext().getResources().getDrawable(R.drawable.icon); 
image.setBounds(0, 0, 60, 60); 
button.setCompoundDrawables(image, null, null, null); 

bu

Güncelleme yapın: getContext().getResources().getDrawable artık

yana kullanımdan kaldırıldı, onun yerine bu kullanın:

Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon); 
    image.setBounds(0, 0, 60, 60); 
    button.setCompoundDrawables(image, null, null, null); 
5

, bu deneyin

Sen kodunun altına
Drawable icon= getContext().getResources().getDrawable(R.drawable.icon); 
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 
-1

kullanabilirsiniz

<Button android:text="@string/button_label" 
android:id="@+id/buttonId" 
android:layout_width="160dip" 
android:layout_height="60dip" 
android:layout_gravity="center" 
android:textSize="13dip" 
android:drawableLeft="@drawable/button_icon" 
android:drawablePadding="2dip" 
android:paddingLeft="30dip" 
android:paddingRight="26dip" 
android:singleLine="true" 
android:gravity="center" /> 
Bu dolgu ti metin ve resim ile yönetecek

hem

+0

Sana cevap yok düşünüyorum soruyu, OP programlı olarak yapmak istedi. – Joffrey

0

Bu deneyebilirsiniz:

txtVw.setCompoundDrawables(R.drawable.img_nature, 0, 0, 0);