使用xml配置如下:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"     android:orientation="vertical"><LinearLayout     android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:orientation="horizontal">      <Button         android:text="@string/btnImgLeft"        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:drawableTop="@drawable/star"        android:drawablePadding="1dp"        />    <Button         android:text="@string/btnImgRight"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableRight="@drawable/star"        android:drawablePadding="1dp"        />     <Button         android:text="@string/btnImgTop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableBottom="@drawable/star"        android:drawablePadding="1dp"        />    <Button         android:text="@string/btnImgBottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/star"        android:drawableBottom="@drawable/star"        android:drawablePadding="1dp"        /> </LinearLayout>     <Button         android:id="@+id/btnBig"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /> </LinearLayout>

java代码实现方式:

package com.example.btncomplext;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.SpannableString;import android.text.Spanned;import android.text.style.DynamicDrawableSpan;import android.text.style.ImageSpan;import android.view.Menu;import android.widget.Button;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) this.findViewById(R.id.btnBig) ;String initLeft = "left";SpannableString spannableLeftString = new SpannableString(initLeft) ;Bitmap bitmapleft = BitmapFactory.decodeResource(getResources(), R.drawable.star);ImageSpan imageSpanLeft =new ImageSpan(MainActivity.this,bitmapleft,DynamicDrawableSpan.ALIGN_BOTTOM);spannableLeftString.setSpan(imageSpanLeft, 0, initLeft.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);String initRigth = "right";SpannableString spannableRigthString = new SpannableString(initRigth) ;Bitmap bitmapright= BitmapFactory.decodeResource(getResources(), R.drawable.star);ImageSpan imageSpanRight =new ImageSpan(MainActivity.this,bitmapright);spannableRigthString.setSpan(imageSpanRight, 0, initRigth.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);btn.append(spannableLeftString);btn.append("我的按钮");btn.append(spannableRigthString);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

更多相关文章

  1. Android 代码模拟输入按键
  2. Android 获取手势onfling代码片段
  3. Android 设置对话框中的确定按钮的 enabled 属性
  4. android —— 常用代码utils
  5. Android中打开扬声器关闭麦克风的代码实现
  6. Android Studio:在xml布局中不能自动补全代码

随机推荐

  1. Cocos2d-x + Android(安卓)+ Eclipse + W
  2. Android布局详解
  3. Android 内存相关 onTrimMemory,onLowMemo
  4. android 开发随笔 - 开发环境搭建
  5. Android 子线程修改UI方法对比
  6. Android ADV 虚拟卡常见错误Failed to pu
  7. Android软件盘(EditText)的搜索功能
  8. Android(安卓)NDK学习 Application.mk简
  9. Android应用程序未捕获异常并提示用户发
  10. Android(安卓)学习资料记录汇总