前言
我们在上一篇文章教大家使用ImageView+TextView的组合自定义控件...可能在开发中你还需要其他功能,例如:按下效果,可以在代码中改变字体颜色,更换图片等等...
首先上效果图,看看是否是你需要的
效果图
下面开始撸代码
MyImageTextView.java
public class MyImageTextView extends LinearLayout { private ImageView mImageView = null; private TextView mTextView = null; private int imageId, pressImageId; private int textId, textColorId, textTopId, pressTextColorId; public MyImageTextView(Context context) { this(context, null); } public MyImageTextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public MyImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.setOrientation(LinearLayout.VERTICAL);//设置垂直排序 this.setGravity(Gravity.CENTER);//设置居中 if (mImageView == null) { mImageView = new ImageView(context); } if (mTextView == null) { mTextView = new TextView(context); } if (attrs == null) return; int count = attrs.getAttributeCount(); for (int i = 0; i
下面是属性文件
image_text.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
属性文件存放位置如下图
文件位置
下面我们来看看具体的调用方法
布局调用
当然我们也可以在Activity中进行再次设置, 例如:
在java中设置
这些都是在自定义View中的set方法...也可以根据具体的业务增删set方法.
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。