2013-08-16 18 views
7

używam przycisku
Programowo zmienić drawableLeft Buttona

<Button 
     android:id="@+id/zoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/trans" 
     android:drawableLeft="@drawable/left_img" 
     android:fontFamily="arial" 
     android:text="My Name is " 
     android:textSize="50sp" /> 

i zmienia swój kolor tekstu z:

zoom.setTextColor(Color.parseColor("voilet")); 

ale nie jest w stanie zrozumieć how to change its image??

+0

przyjrzeć się http://developer.android.com/guide/topics/ui/controls /button.html#CustomBackground do stylu przycisku – Eluvatar

+0

Ustaw podobny przycisk.setBackground (R.drawable.ic_launcher); wewnątrz detektora onclick. – Aravin

+0

Możliwy duplikat [Jak programowo ustawić opcję drawableLeft na urządzeniu z Androidem?] (Http://stackoverflow.com/questions/4502605/how-to-programically-set-drawableleft-on-android-button) – Varun

Odpowiedz

38

Spróbuj tego:

int imgResource = R.drawable.left_img; 
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0); 

Reference

0

I polecam, że zamiast używać przycisku, korzystasz z Imageview i dodajesz do niego detektor onclick. W ten sposób można po prostu zrobić Imageview.setbitmap(bitmap) i stworzyć bitmapę z jednego z kanału alfa

2

Aby to zrobić, można użyć tych

setCompoundDrawables (...);

metoda. Należy pamiętać, że jest on wyposażony w przycisk TextView, a nie w przycisk.

To jak go używać:

Drawable img = getContext().getResources().getDrawable(R.drawable.yourimage); 
img.setBounds(0, 0, 60, 60); // set the image size 
txtVw.setCompoundDrawables(img, null, null, null); 

albumu: How to programmatically set drawableLeft on Android button?

+0

Jak ustawić obraz w Rightside z textview –

12

Najbezpieczniej ustawić lewy rozciągliwej bez zmiana wartości pozostałych elementów (górny, prawy i na dole):

Drawable[] drawables = textViewExample.getCompoundDrawables(); 
textViewExample.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, drawables[1], drawables[2], drawables[3]); 
+0

ładny punkt!, Dzięki !!. – Bhimbim

0

wystarczy wykonać ten kod mam nadzieję, że to bardzo pomocne dla Ciebie ..

boolean isIconChange; 
button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     isIconChange = !isIconChange; 
     if(isIconChange){ 
      button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like, 0, 0, 0); 
      button.setTextColor(Color.BLACK); 
     } else { 
      button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dislike, 0, 0, 0); 
      button.setTextColor(Color.RED); 
     } 
    } 
}); 
Powiązane problemy