Android自定义按钮
来源:互联网 发布:极客学院 php 百度云 编辑:程序博客网 时间:2024/06/14 03:40
系统的Button不支持文字和图片同时上下显示,可以做,但是无法支持控制图片大小,自定义代码如下:
1、建立按钮的内部布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="35dp" //设置图片大小 android:layout_height="35dp" android:layout_gravity="center"/> <TextView android:id="@+id/textView1" android:layout_width="70dp" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textColor="#ffffff" android:layout_marginBottom="2dp" /></LinearLayout>
2、代码实现:
public class ImageBtn extends LinearLayout{private ImageView imageView; private TextView textView; public ImageBtn(Context context) { super(context); // TODO Auto-generated constructor stub } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.imagebutton, this); imageView=(ImageView) findViewById(R.id.imageView1); textView=(TextView)findViewById(R.id.textView1); } /** * 设置图片资源 */ public void setImageResource(int resId) { imageView.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text) { textView.setText(text); } public void setTextViewText(int resId){ textView.setText(resId); }}
<com.microntek.bluetooth.ui.ImageBtn android:id="@+id/pub_dial" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/left_btn_bac" android:gravity="center_vertical" />
4、在主代码中调用:
pubdial = (ImageBtn)mActivity.findViewById(R.id.pub_dial);
pubdial.setTextViewText(R.string.dailButton);//设置文字pubdial.setImageResource(R.drawable.ico_dial);//设置图片
0 0
- android 自定义返回按钮
- 编写Android自定义按钮
- Android自定义按钮样式
- Android自定义返回按钮
- Android自定义按钮
- Android自定义Button按钮
- android 自定义按钮样式
- Android自定义开关按钮
- Android自定义按钮
- Android的自定义按钮
- android自定义按钮类
- android 自定义开关按钮
- Android 自定义按钮
- Android自定义控件按钮
- Android自定义开关按钮
- android 自定义切换按钮
- android 自定义按钮控件
- android 自定义开关按钮
- 【Nodejs】Pomelo:网易开源基于游戏服务端框架安装
- IEnumerable和IEnumerator 详解
- docker使用的一些建议
- java中的String.split() 中“|”作为分隔符的问题和数组长度问题
- TCP封装的隧道对于拥塞控制的意义
- Android自定义按钮
- javascript实现动态模态绑定grid
- 创建一个以JDBC链接数据库的程序,包含7个步骤
- setInterval() 方法
- C/C++中static关键字作用总结
- maven项目导入到Myeclipse中,以及后续配置
- 利用SecureCRT上传、下载文件(使用sz与rz命令)
- 基金净值文档
- android UI进阶之自定义组合控件