安卓Button图文混排
来源:互联网 发布:淘宝网食品流通许可证 编辑:程序博客网 时间:2024/05/17 12:02
Ui设计
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="120dp" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/star" android:text="按钮1" > </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="10dp" android:drawableTop="@drawable/star" android:text="按钮2" > </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/star" android:drawableTop="@drawable/star" android:text="按钮3" > </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="30dp" android:drawableRight="@drawable/star" android:text="按钮4" > </Button> </LinearLayout> <Button android:id="@+id/button" android:layout_width="200dp" android:layout_height="200dp" android:layout_marginTop="10dp" > </Button></LinearLayout>
Main
package com.android.mytextimagebutton;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.text.SpannableString;import android.text.Spanned;import android.text.style.DynamicDrawableSpan;import android.text.style.ImageSpan;import android.widget.Button;public class Main extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button = (Button) this.findViewById(R.id.button);SpannableString spannableStringLeft = new SpannableString("left");//获取图像资源Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(),R.drawable.image_left);//ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft,//DynamicDrawableSpan.ALIGN_BOTTOM);ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft);spannableStringLeft.setSpan(imageSpanLeft, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);SpannableString spannableStringRight = new SpannableString("right");Bitmap bitmapRight = BitmapFactory.decodeResource(getResources(),R.drawable.image_right);ImageSpan imageSpanRight = new ImageSpan(bitmapRight);spannableStringRight.setSpan(imageSpanRight, 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);button.append(spannableStringLeft);button.append("\n");button.append("我的按钮\n");button.append(spannableStringRight);}}
知识点
1.XML文件中可实现Button按钮的图文混排
android:drawableTop设置文字上方显示的图片
android:drawableBottom设置文字下方显示的图片
android:drawableLeft显示文字左边显示的图片
android:drawableRight显示文字右边显示的图片
2.java代码中我们也可以通过使用SpannableString类来实现Button按钮的图文混排效果,具体实现步骤如下:
首先,我们需要创建一个SpannableString对象,目的是用来插入用ImageSpan对象封装好的图像。
其次,我们还需要创建一个Bitmap对象,并通过资源Id取得要使用的图像。再将该Bitmap图像用ImageSpan对象封装起来。
然后,通过使用SpannableString类的setSpan()方法加载该ImageSpan对象。
最后,通过使用Button控件的append()方法来加载SpannableString对象即可。
3.Button的常用事件监听器有以下一些:
mButton.setOnClickListener();//点击事件监听器
mButton.setOnTouchListener();//触摸事件监听器
mButton.setOnFocusChangeListener();//焦点状态改变事件监听器
mButton.setOnKeyListener();//按键事件监听器
mButton.setOnLongClickListener();//常压事件监听器
这些事件监听器可以用来响应对Button按钮的不同操作,使用方法比较简单,此处就不多介绍了。
- 安卓Button图文混排
- Button 图文混排
- 图文混排Button
- 安卓控件使用系列9:Button实现图文混排
- Button的上下图文混排
- 安卓:自定义适配器实现GridView图文混排
- 安卓中的图文混排 ImageSpan SpannableStringBuffer
- 安卓原生开发图文混排显示
- Android学习备忘017——Button图文混排
- 安卓控件使用系列2:TextView实现图文(图片和文字)混排
- 图文混排
- 图文混排
- 图文混排
- CSS图文混排
- 图文混排TLFTextField
- 图文混排
- TTStyledTextLabel 图文混排
- FloatImageText图文混排
- Jquery 验证注册
- iOS ASIHTTPRequest详解
- 如何处理handler对activity的持有
- 草屋内异常的贫寒,连个像样的桌子都没有
- Flex ComboBox不关联数据问题
- 安卓Button图文混排
- 实例--聊天室
- Windows环境下搭建基于Eclipse的STM32 GCC 开发环境(一)工具篇
- 学习笔记 --- LINUX 驱动调试之使用proc
- XCode编译器介绍
- magento前台展示product的各种信息
- 编程的一个关键是管理并控制好复杂性
- android开发一些小知识点记录
- ffmpeg ubuntu 官方编译