2011-07-02 11 views
13

Obecnie moja szuflada po prostu dostosowuje się do jej normalnych rozmiarów, chcę, aby pasowała do mojego przycisku. Oto obraz tego, jak wygląda teraz:Skalowanie rysowanego wewnątrz przycisku?

enter image description here

Oto xml dla przycisku:

<LinearLayout android:layout_width="wrap_content" 
    android:id="@+id/linearLayout2" android:layout_height="40dp" 
    android:layout_weight="0.4" android:gravity="right|center_vertical" 
    android:paddingRight="5dp"> 
    <Button android:id="@+id/button1" android:background="@drawable/refresh" 
     android:layout_height="25dp" android:layout_width="150dp" 
     android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"></Button> 
</LinearLayout> 

Więc chcę, żeby wyglądać tak:

enter image description here

Czy istnieje sposób, aby zmniejszyć mój drawable?

Odpowiedz

2

Odtworzenie obrazu. Zachowaj rozmiar obrazu w pikselach, ale zmniejsz rozmiar strzałki i pozostaw resztę przezroczystą. To mój zakład w każdym razie =)

+0

Wygląda na to, że muszę z tym wyjść. –

+1

Dlaczego piksele są lepsze niż zanurzenie? –

+0

Myślę, że źle zrozumiałeś moją odpowiedź: nie mam na myśli, że piksele są lepsze niż dip. Kiedy napisałem "Zachowaj rozmiar obrazu w pikselach ... []" mam na myśli rzeczywisty obraz .png, a nie jakiś element xml. – Emiam

0

Spróbuj

<Button android:id="@+id/button1" android:background="@drawable/refresh" 
    android:layout_height="25dp" android:layout_width="150dp" 
    android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48" 
    android:padding="5dp"></Button> 

(So dodać android:padding="5dp")

+0

Teraz wygląda to tak: http://i.imgur.com/7DJL3 .png –

0

Zrobiłem to! Trochę bałaganu i ostatecznie nie korzystałem z widoku przycisków. Sam "button" jest relatywnym outoutem, więc jeśli chcesz użyć tego rozwiązania, będziesz musiał skrzypce z niektórymi, jeśli chcesz, aby przycisk był animowany. Tu jest mój XML:

<LinearLayout android:layout_width="wrap_content" 
    android:id="@+id/linearLayout2" android:layout_height="40dp" 
    android:layout_weight="0.4" android:gravity="right|center_vertical" 
    android:paddingRight="5dp"> 
    <RelativeLayout android:layout_height="25dp" 
     android:layout_width="150dp" android:id="@+id/relativeLayout1" 
     android:clickable="true"> 
     <ImageView android:id="@+id/imageView1" android:scaleType="fitXY" 
      android:adjustViewBounds="true" android:layout_width="fill_parent" 
      android:src="@drawable/saywhat" android:layout_height="fill_parent"></ImageView> 
     <LinearLayout android:layout_width="fill_parent" 
      android:id="@+id/linearLayout3" android:weightSum="1" 
      android:layout_height="fill_parent"> 
      <TextView android:layout_weight="0.6" 
       android:layout_height="fill_parent" android:text="TextView" 
       android:layout_width="wrap_content" android:id="@+id/textView1"></TextView> 
      <LinearLayout android:layout_weight="0.4" 
       android:layout_height="fill_parent" android:layout_width="wrap_content" 
       android:id="@+id/linearLayout4"> 
       <ImageView android:id="@+id/imageView2" 
        android:layout_height="wrap_content" android:scaleType="fitXY" 
        android:adjustViewBounds="true" android:layout_width="wrap_content" 
        android:src="@drawable/refresh_48"></ImageView> 
      </LinearLayout> 
     </LinearLayout> 
    </RelativeLayout> 
</LinearLayout> 

Enjoy :)

+8

To bardzo niepotrzebny skomplikowany przycisk: P – Jona

+1

To byłby zbyt zły przycisk. –

+8

Ponownie odwiedzając tę ​​odpowiedź po wielu latach doświadczeń z systemem Android, mogę śmiało powiedzieć, że nie powinieneś nawet myśleć o jej implementacji w ten sposób. –

3

Ponieważ nie można modyfikować lub skalowanie obrazu na xml trzeba ustawić złożonych rozciągliwej granic w kodzie.

Zastąp klasę przycisków własnym "przyciskiem niestandardowym". Następnie zastąpić „SetCompoundDrawables metodą (lewy, górny, prawy i dolny).

public override void SetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 
    { 
     if (right != null) 
     { 
      var bounds = right.Bounds; 
      right.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70); 
     } 

     if (left != null) 
     { 
      var bounds = left.Bounds; 
      left.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70); 
     } 

     base.SetCompoundDrawables(left, top, right, bottom); 
    } 

Należy pamiętać, że jest to C# Code ponieważ używam Xamarin ale kod Java powinny być takie same (pomimo pewnego uppper case namings)

1

Chyba znalazłem rozwiązanie problemu, bardzo późno, ale może to pomóc innym

Drawable drawable = getResources().getDrawable(R.drawable.s_vit); 
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
        (int)(drawable.getIntrinsicHeight()*0.5)); 
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight); 
Button btn = findViewbyId(R.id.yourbtnID); 
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 
4

Problem ten można rozwiązać programowo: Zapraszamy do obejrzenia funkcji scaleButtonDrawables w this answer Dzięki tej funkcji. możesz pisać w onCreate() swojej działalności:

final Button button = (Button) findViewById(R.id.button1); 
ViewTreeObserver vto = button.getViewTreeObserver(); 
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
      public boolean onPreDraw() { 
       button.getViewTreeObserver().removeOnPreDrawListener(this); 
       //scale to 90% of button height 
       DroidUtils.scaleButtonDrawables(button, 0.9); 
       return true; 
      } 
     }); 

(. Nie można nazwać tego bezpośrednio, ponieważ wysokość Przycisk nie jest dostępny w onCreate())

+0

Dziękujemy! Prace. – DmitryKanunnikoff

Powiązane problemy