使用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 { @Override protected 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); } @Override public 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; } }
相关推荐
《Android开发视频教程》第九集:Button图文混排的按钮.zip
在Android应用程序中,Button控件是最常用的组件之一,而在实际开发中,我们经常需要在Button上显示图文混排效果,例如在按钮上添加图片、文字等 Element。今天,我们主要来讨论如何在Android下实现Button的图文混排...
【Android高级应用源码-仿QQ聊天界面,可发png,gif,图文混排】 这个项目是针对Android平台的一个高级应用示例,旨在模仿QQ聊天界面的功能和用户体验。通过此源码,开发者可以学习到如何在Android应用中实现丰富的...
在实际项目中,你可以根据需要进一步扩展这个类,比如添加动画效果、图文混排等功能,以满足各种复杂的界面设计需求。 在压缩包文件中,你将找到`CustomButton.java`源代码文件以及一个使用这个自定义按钮的示例...
6.9 Button图文混排的按钮 6.10 RadioButton单选按钮的使用 6.11 ToggleButton按钮的使用 6.12 CheckBox复选框控件使用 6.13 SeekBar拖动控件的使用 6.14 ImageView的基本用法 6.15 ImageView实现适屏和裁剪图片 ...
首先需要了解的是,在Android开发中,TextVIew和EditText控件都支持图文混排的显示效果。然而,TextView仅用于显示,而EditText则不仅可显示图文混排效果,还可以让用户输入文本。为了让用户在输入时既可以输入文字...