zoukankan      html  css  js  c++  java
  • 在Button任意位置加图片效果

    通常情况会想到drawableLeft,drawableTop,drawableRight,drawableBotton,这样是可以做,但只能在四个位置。 
    下面这种办法可以实现任意位置: 

    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.text.Html;
    import android.text.Spanned;
    import android.text.Html.ImageGetter;
    import android.widget.Button;

    public class demobutton extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageGetter imgGetter = new Html.ImageGetter() {
    @Override
    public Drawable getDrawable(String source) {
    Drawable drawable = null;
    drawable = demobutton.this.getResources().getDrawable(
    Integer.parseInt(source));
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
    drawable.getIntrinsicHeight());
    return drawable;
    }
    };

    Button btn = (Button) findViewById(R.id.button1);
    StringBuffer sb = new StringBuffer();
    sb.append("<img src=\"").append(R.drawable.button1).append("\"/>").append("<font color=\"6f6f6f\">按钮上加图片效果</font>");;
    Spanned span = Html.fromHtml(sb.toString(), imgGetter, null);
    btn.setText(span);
    sb = null;
    }
    }


     
    采用Spanned也不是什么新鲜事了,呵呵。这里只提供一种思路。不止Button,任意可以显示文字的View都可以这样做。

    转自:http://gundumw100.iteye.com/blog/1205578


  • 相关阅读:
    分治法求最大子序列
    6.2 链表 (UVa 11988, 12657)
    6.1 栈和队列 (UVa 210, 514, 442)
    S-Tree (UVa 712) 二叉树
    Equilibrium Mobile (UVa 12166) dfs二叉树
    Patrol Robot (UVa 1600) BFS
    Knight Moves (UVa 439) BFS
    Tree Recovery (UVa 536) 递归遍历二叉树
    Parentheses Balance (Uva 673) 栈
    Self-Assembly (UVa 1572)
  • 原文地址:https://www.cnblogs.com/shanzei/p/2415725.html
Copyright © 2011-2022 走看看