2011-07-02 154 views
13

目前我的drawable只是縮放到正常尺寸,我想讓它適合我的按鈕。下面是現在的樣子的照片:在按鈕內縮放drawable?

enter image description here

下面是按鈕的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"> 
    <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> 

所以我希望它看起來像這樣:

enter image description here

有什麼方法可以縮小我的drawable?

回答

2

重新創建圖像。保持圖像大小的像素,但減少箭頭的大小,並使其餘透明。這是我的賭注無論如何=)

+0

看起來我必須去那個。 –

+1

爲什麼像素比傾角好? –

+0

我認爲你誤解了我的答案:我不是說像素比傾角要好。當我寫下「保持圖像大小像素... []」我的意思是實際的.png圖像,而不是一些XML元素。 – Emiam

0

嘗試

<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> 

(所以加android:padding="5dp"

+0

現在,它看起來像這樣:http://i.imgur.com/7DJL3 .png –

0

我做到了!這有點麻煩,我最終沒有使用按鈕視圖。 「按鈕」本身是一個relativeLayout,所以如果你想使用這個解決方案,你將不得不擺弄一些來如果你想按鈕動畫。下面是我的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> 

享受:)

+8

這是一個非常不必要的複雜按鈕:P – Jona

+1

這將是一個太邪惡的按鈕。 –

+8

經過多年Android體驗後重新訪問此答案我可以放心地說,你甚至不應該考慮以這種方式實現它。 –

3

既然你不能修改或擴展基於XML,你需要在代碼中設置該化合物可繪製邊界圖像。

用你自己的「CustomButton」覆蓋Button類。然後覆蓋的方法「SetCompoundDrawables(左,上,右,下):

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); 
    } 

請注意,這是C#代碼,因爲我使用Xamarin但Java代碼應該是一樣的(儘管有些uppper的情況namings)

1

我想我找到了你的問題的解決方案,非常晚,但它可能會幫助別人

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

您可以通過編程解決這個問題:有一個在功能scaleButtonDrawablesthis answer與該功能。你可以寫入您的活動onCreate()

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; 
      } 
     }); 

(你不能把這個直接,因爲按鈕的高度不可用的onCreate())

+0

謝謝!作品。 – DmitryKanunnikoff