2012-12-18 132 views
3

在看到此功能的許多問題並試圖回答問題後,我仍然想知道是否有更清晰的示例?帶有文本和圖像的Android按鈕

編輯:我試圖做一個大的按鈕,有一個圖像和文字,在'中間'。它必須表現爲一個按鈕(StateList繪製)和圖像/文本對應該進行分組和中心(作爲一組)

+0

我想知道你是否想要一個文字和圖像並排放置在圖像上或者文字要覆蓋在圖像上 –

+0

@Corey Scott如果我給出的解決方案可以幫助您解決問題,那麼您可以接受我的解決方案。謝謝。 –

回答

2

在試圖救別人一些時間,我提供這樣的:

佈局/ some_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/menu_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    <!-- StateList Drawable to make it look like a button --> 
    android:background="@drawable/btn_std_holo_states" 
    <!-- Required so you can click on it like a button --> 
    android:clickable="true"  
    <!-- Recommended min height from the guidelines --> 
    android:minHeight="48dp"  
    <!-- OnClickEvent definition --> 
    android:onClick="onClickOk" > 

    <!-- Compound drawable of graphic and text --> 
    <TextView 
     android:id="@+id/txt_ok" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     <!-- Center both the graphic and text inside the button --> 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     <!-- Draw the graphic to the left of the text --> 
     android:drawableLeft="@drawable/ic_ok" 
     <!-- Space between the graphic and the text--> 
     android:drawablePadding="16dp" 
     <!-- ensures the text and graphic are both centered vertically --> 
     android:gravity="center" 
     <!-- Text of the button --> 
     android:text="@android:string/ok" 
     <!-- Change the font to match the standard button settings (optional) --> 
     android:textAppearance="?android:attr/textAppearanceButton" /> 

</RelativeLayout> 

繪製/ btn_std_holo_states.xml(上面提到的)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/abs__btn_cab_done_pressed_holo_dark" android:state_pressed="true"/> 
    <item android:drawable="@drawable/abs__btn_cab_done_focused_holo_dark" android:state_enabled="true" android:state_focused="true"/> 
    <item android:drawable="@android:color/transparent" android:state_enabled="true"/> 
    <item android:drawable="@android:color/transparent"/> 

</selector> 

注:不同@drawable和@android:這裏顏色設置可以是任何東西和再僅供做出一個完整的例子

+1

如果你可以使用'Button',爲什麼在'RelativeLayout'中包裝'TextView'?由於'Button擴展了TextView',它支持複合drawables沒有問題。你提出的建議看起來相當麻煩和低效。 –

+0

當您使用按鈕時,drawableLeft會在按鈕的邊緣繪製圖形。我所尋找的是圖形更加集中的東西。是的,你可以使用邊距/填充來移動drawable,但是會影響按鈕的區域,導致其他問題 –

+0

使用RelativeLayout和TextView來裝飾帶圖像的Button並不是一個矯枉過正的事情嗎?這樣的裝飾可以很容易地完成一個自定義Drawable - 看看我的答案在這裏 – pskink

0

試試這個:

Drawable appImg = getApplicationContext().getResources().getDrawable(R.drawable.ic_launcher); 
appImg.setBounds(0, 0, appImg.getIntrinsicHeight(), appImg.getIntrinsicWidth()); 

Button btn_ok = (Button) findViewById(R.id.ok); 
btn_ok.setCompoundDrawables(null, null, appImg, null); 

希望它可以幫助你。

謝謝。

1

試試這個自定義繪製對象:

class BackgroundDrawable extends StateListDrawable { 
    private StateListDrawable mDrawable; 
    private Bitmap mBitmap; 
    private Matrix mMatrix; 
    private boolean mScale; 
    private int mGravity; 
    private int mDx; 
    private int mDy; 

    public BackgroundDrawable(StateListDrawable sld, Resources res, int resId, boolean scale, int gravity, int dx, int dy) { 
     mDrawable = sld; 
     mBitmap = BitmapFactory.decodeResource(res, resId); 
     mMatrix = new Matrix(); 
     mScale = scale; 
     mGravity = gravity; 
     mDx = dx; 
     mDy = dy; 
    } 

    public static void setupBackground(View v, int resId, boolean scale, int gravity, int horizontalPadding, int verticalPadding) { 
     Drawable d = v.getBackground(); 
     if (d instanceof StateListDrawable) { 
      StateListDrawable sld = (StateListDrawable) d; 
      Drawable drawable = new BackgroundDrawable(sld, v.getResources(), resId, scale, gravity, horizontalPadding, verticalPadding); 
      v.setBackgroundDrawable(drawable); 
     } 
    } 

    @Override 
    protected boolean onStateChange(int[] stateSet) { 
     invalidateSelf(); 
     return super.onStateChange(stateSet); 
    } 

    @Override 
    protected void onBoundsChange(Rect bounds) { 
     mDrawable.setBounds(bounds); 
     Rect b = new Rect(bounds); 
     b.inset(mDx, mDy); 
     RectF src = new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 
     RectF dst = new RectF(b); 
     float[] values = new float[9]; 
     if (mScale) { 
      mMatrix.setRectToRect(src, dst, ScaleToFit.START); 
     } 
     mMatrix.getValues(values); 
     float sx = values[Matrix.MSCALE_X]; 
     float sy = values[Matrix.MSCALE_Y]; 
     Rect outRect = new Rect(); 
     Gravity.apply(mGravity, (int) (src.width() * sx), (int) (src.height() * sy), b, outRect); 
     mMatrix.postTranslate(outRect.left, outRect.top); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     int[] stateSet = getState(); 
     mDrawable.setState(stateSet); 
     mDrawable.draw(canvas); 
     canvas.drawBitmap(mBitmap, mMatrix, null); 
    } 
} 

以及如何使用它:

Button b0 = (Button) findViewById(R.id.b0); 
BackgroundDrawable.setupBackground(b0, R.drawable.ic_launcher, false, Gravity.BOTTOM | Gravity.RIGHT, 10, 5); 
Button b1 = (Button) findViewById(R.id.b1); 
BackgroundDrawable.setupBackground(b1, R.drawable.ic_launcher, false, Gravity.TOP, 0, 0);