如题  要一个整体的按钮效果  但是左面有个小图片 右面是文本   这个应该如何实现呢?

解决方案 »

  1.   

    http://fanwei51880.blog.163.com/blog/static/324067402010102361017173/
    这个 比较容易实现
      

  2.   

        访问不了外网,CSDN你是怎么访问的?
      

  3.   

    好吧。如下:方法一:直接用图片和文字的截图放在Imagebutton上显示,这种事最简单但是也是最占资源,以后修改和国家化都是最不方便的方法方法二:用button上设置图片的方法可以,这种简单,并且以后很容易修改<Button android:drawableTop="@drawable/****"> 方法三:textview.setCompoundDrawables(icon, null, null, null);方法四:重写onDraw函数:
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.view.MotionEvent;
    import android.view.View;public class ImageTextButton extends View{private final static int WIDTH_PADDING = 8;
    private final static int HEIGHT_PADDING = 10;
    private final static int SPACE = 10;
    private final String label;
    private final int imageResId;
    private final Bitmap image;
    private int fontWidth;
    private int fontHeight;public ImageTextButton(final Context c,int rid,String text){
    super(c);
    this.label = text;
    this.imageResId = rid;
    this.image = BitmapFactory.decodeResource(c.getResources(),imageResId);
    setFocusable(true);
    setClickable(true);
    getFontWidthAndHeight();
    }private void getFontWidthAndHeight(){
    Paint pFont = new Paint(); 
    Rect rect = new Rect(); 
    pFont.getTextBounds("信", 0, 1, rect); 
    this.fontHeight = rect.height();
    this.fontWidth = rect.width();
    }private int getTextWidth(String text){
    return text.length()*this.fontWidth;
    }@Override
    protected void onFocusChanged(boolean gainFocus, int direction,
    Rect previouslyFocusedRect) {
    if (gainFocus == true){
    this.setBackgroundColor(Color.rgb(255, 165, 0));
    }
    else{
    this.setBackgroundColor(Color.alpha(0));
    }
    }@Override
    protected void onDraw(Canvas canvas) {
    Paint textPaint = new Paint();
    textPaint.setColor(Color.WHITE);
    canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);
    canvas.drawText(label, (image.getWidth()-getTextWidth(label)/2)/ 2, (HEIGHT_PADDING / 2) +
    image.getHeight() + 8+SPACE, textPaint);
    }@Override
    public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    switch(action){
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
    this.setBackgroundColor(Color.rgb(255, 165, 0));
    break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
    this.setBackgroundColor(Color.alpha(0));
    break;
    }return super.onTouchEvent(event);
    }@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
    }private int measureWidth(int measureSpec){
    int preferred = image.getWidth() * 2;
    return getMeasurement(measureSpec, preferred);
    }
    private int measureHeight(int measureSpec){
       int preferred = image.getHeight()+this.fontHeight+SPACE*2;
       return getMeasurement(measureSpec, preferred);
    }private int getMeasurement(int measureSpec, int preferred){
      int specSize = MeasureSpec.getSize(measureSpec);
    int measurement = 0;
    switch(MeasureSpec.getMode(measureSpec)){
    case MeasureSpec.EXACTLY:
    measurement = specSize;
    break;
    case MeasureSpec.AT_MOST:
    measurement = Math.min(preferred, specSize);
    break;
    default:
    measurement = preferred;
    break;
    }
    return measurement;
    }public String getLabel(){
    return label;
    }  public int getImageResId(){
    return imageResId;
    }}