【组合控件】android自定义控件之带文字的ImageView
2016-03-15 21:28
681 查看
android自带的ImageView控件是不能添加文字的,如果出现大量ImageView和TextView同时出现的布局,可以组合定义一个控件,将ImageView和TextView组合成一个控件
如下图所示:
布局文件中这么使用:
图片将通过自定义的custom:picture传递给ImageView控件,文字将通过android:text传递给TextView
如下图所示:
public class ImageButtonWithText extends LinearLayout { public ImageView imageView; public TextView textView; public ImageButtonWithText(Context context,AttributeSet attrs) { super(context,attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageButtonWithText); /* * 在attrs.xml添加属性: * <declare-styleable name="ImageButtonWithText"> <attr name="picture" format="reference"/> </declare-styleable> * */ int picture_id = a.getResourceId(R.styleable.ImageButtonWithText_picture,-1); /** * Recycle the TypedArray, to be re-used by a later caller. After calling * this function you must not ever touch the typed array again. */ a.recycle(); imageView = new ImageView(context, attrs); imageView.setPadding(10, 10, 10, 10); /** * Sets a drawable as the content of this ImageView. * This does Bitmap reading and decoding on the UI * thread, which can cause a latency hiccup. If that's a concern, * consider using setImageDrawable(android.graphics.drawable.Drawable) or * setImageBitmap(android.graphics.Bitmap) instead. * 直接在UI线程读取和解码Bitmap,可能会存在潜在的性能问题 * 可以考虑使用 setImageDrawable(android.graphics.drawable.Drawable) * 或者setImageBitmap(android.graphics.Bitmap) 代替 */ imageView.setImageResource(picture_id); textView =new TextView(context, attrs); /** * Sets the horizontal alignment of the text and the * vertical gravity that will be used when there is extra space * in the TextView beyond what is required for the text itself. */ //水平居中 textView.setGravity(android.view.Gravity.CENTER_HORIZONTAL); textView.setPadding(0, 0, 0, 0); setClickable(true); setFocusable(true); setOrientation(LinearLayout.VERTICAL); addView(imageView); addView(textView); } public void setText(int resId) { textView.setText(resId); } public void setText(CharSequence buttonText) { textView.setText(buttonText); } public void setTextColor(int color) { textView.setTextColor(color); }
布局文件中这么使用:
<com.uestcneon.chuji.changjianglife.share.ImageButtonWithText xmlns:custom="http://schemas.android.com/apk/res-auto" style="@style/hisCardTable" custom:picture="@mipmap/his_card_company" android:id="@+id/imgbtn_company" android:text="公司(1)" > </com.uestcneon.chuji.changjianglife.share.ImageButtonWithText>
图片将通过自定义的custom:picture传递给ImageView控件,文字将通过android:text传递给TextView
相关文章推荐
- 解决android项目api高于模拟器api…
- android使用单选并设置按钮提醒
- android使用单选并设置按钮提醒2
- android第五章实例问题——屏幕转成…
- android 路径地址与Uri的相互转换 uri转string
- Android-WindowManager
- The first day to learn Android
- Android常见的问题
- Android待机及延时网路关闭的修改
- android-个性化进度条
- Android.mk小结
- 轻松学习Ionic (二) 为Android项目集成Crosswalk(更新官方命令行工具)
- Android源代码分析之Framework的MediaPlayer
- 属性动画,补间动画,帧动画
- Android ListView控件的使用(自定义适配器 + ListView + 解决OOM )
- 开机自启动和多进程守护
- Android开发艺术-第一章Activity的生命周期和启动模式
- Android计算两个时间戳的差值,以字符串的形式表示
- ANDROID STUDIO “CANNOT RESOLVE SYMBOL” 解决办法
- Android Studio 如何Debug