android控件---自定义带文本的ImageButton
2014-08-13 14:07
309 查看
由于SDK提供的ImageButton只能添加图片,不能添加文字;而Button控件添加的文字只能显示在图片内部;当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageButton。说是ImageButton,其实并不是继承于ImageButton,而是从LinearLayout继承,由于LinearLayout是线性排列,通过setOrientation(LinearLayout.VERTICAL)的方式达到View垂直排列的目的,所以很简单,只需要添加两个View:一个ImageView和一个TextView即可。代码如下:
添加一个LinearLayout作为ImageButtonEx的容器。
然后再Activity中添加调用代码:
package com.example.adtest; import android.content.Context; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ImageButtonEx extends LinearLayout { private ImageView _imageView = null; private TextView _textView = null; public ImageButtonEx(Context context){ super(context); } public ImageButtonEx(Context context, int imageResId, int textResId) { super(context); // TODO Auto-generated constructor stub _imageView = new ImageView(context); _textView = new TextView(context); _imageView.setImageResource(imageResId); _textView.setText(textResId); _imageView.setPadding(0, 0, 0, 0); _textView.setPadding(0, 0, 0, 0); setClickable(true); setFocusable(true); setBackgroundResource(android.R.drawable.btn_default); setOrientation(LinearLayout.VERTICAL); addView(_imageView); addView(_textView); } public void setBtnText(CharSequence text) { _textView.setText(text); } }
添加一个LinearLayout作为ImageButtonEx的容器。
<LinearLayout android:id="@+id/llImageBtnEx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"/>
然后再Activity中添加调用代码:
package com.example.adtest; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageButtonEx imageBtnEx = new ImageButtonEx(this, R.drawable.icon, R.string.hello_world); imageBtnEx.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub imageBtnEx.setBtnText("已经点击过了"); }}); LinearLayout llimageBtnEx = (LinearLayout)findViewById(R.id.llImageBtnEx); llimageBtnEx.addView(imageBtnEx); } }
![](http://images.cnitblog.com/i/656750/201408/131410010148562.jpg)
相关文章推荐
- Android代码中设置图片,文本自定义颜色,Linearlayout等控件设置背景
- 关于android的imagebutton,imageview等无文本控件警告的解决办法//android:contentDescription="@string/desc"
- Android 自定义变化的文本控件ColorChangeView
- android 自定义文本组合控件
- Android_自定义控件之设置文本的大小
- Android 控件背景图片和imagebutton的使用
- 自定义View控件解决android文字排版和换行的问题
- 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息
- 【Android游戏开发二十三】自定义ListView【通用】适配器并实现监听控件!
- 自定义文字滚动控件,用于单行文本的显示,当文字过长的时候,就会自动的向左滚动
- INFO:InstallShield InstallScript工程中自定义界面文本输入控件的两个注意事项
- 【Android游戏开发二十三】自定义ListView【通用】适配器并实现监听控件!
- 在vb中实现真正锁定的带自定义菜单的文本控件
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- Android文字跑马灯控件(文本自动滚动控件)
- InstallShield InstallScript工程中自定义界面文本输入控件的两个注意事项
- [导入]自定义文本编辑控件中的光标控制
- ASP.NET基础教程-Web 自定义控件的使用-在页面上根据控件自定义属性的值在页面上连续输出文本
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
- Android 控件背景图片和imagebutton的使用(selector)