自定义属性之LinearLayout ImageView TextView模拟图片文字按钮
一、资源文件:
1、文字选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#FF111111"/>
<!-- pressed -->
<item android:state_focused="true" android:color="#FF222222"/>
<!-- focused -->
<item android:state_selected="true" android:color="#FF333333"/>
<!-- selected -->
<item android:state_active="true" android:color="#FF444444"/>
<!-- active -->
<item android:state_checkable="true" android:color="#FF555555"/>
<!-- checkable -->
<item android:state_checked="true" android:color="#FF666666"/>
<!-- checked -->
<item android:state_enabled="true" android:color="#FF777777"/>
<!-- enabled -->
<item android:state_window_focused="true" android:color="#FF888888"/>
<!-- window_focused --> </selector>
2、背景选择器:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" > <stroke
android:width="0.5dip"
android:color="#ff9d9d9d" /> <corners android:radius="2dip" >
</corners> <padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip"/> <gradient android:startColor="#ff9d9d9d"
android:centerColor="#ff9e9e9e"
android:endColor="#ff9d9d9d"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" > <stroke
android:width="0.5dip"
android:color="#ff505050" /> <corners android:radius="2dip" >
</corners> <padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip"/> <gradient android:startColor="#ff404040"
android:centerColor="#ff383838"
android:endColor="#ff404040"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/item_background" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/item_background_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/item_background" android:state_focused="true"/>
<item android:drawable="@drawable/item_background"/> </selector>
3、属性文件:
<!-- imageview text -->
<declare-styleable name="ImageViewText">
<attr name="image_size" format="dimension" />
<attr name="image_src" format="reference" />
<attr name="textSize" format="dimension" />
<attr name="text" format="string" />
<attr name="textMargin" format="dimension" />
<attr name="textColor" format="reference" />
<!-- 取值 left top right bottom -->
<attr name="text_direction" format="string" />
<attr name="state_normal" format="color" />
<attr name="state_pressed" format="color" />
<attr name="state_selected" format="color" />
<attr name="view_background" format="reference" />
</declare-styleable>
二、自定义图片文字:
1、采用后台代码实现:
public class ImageViewText extends LinearLayout {
private ImageView mImageView;
private TextView mTextView;
private View view;
public ImageViewText(Context context) {
super(context);
initView(context, null);
}
public ImageViewText(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
mImageView = new ImageView(context);
mTextView = new TextView(context);
view = this;
view.setBackgroundColor(Color.GRAY);
this.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
this.setPadding(5, 5, 5, 5);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(ocl);
// view.setOnTouchListener(otl);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ImageViewText);
float textSize = typedArray.getDimension(R.styleable.ImageViewText_textSize, 14);
Logger.getLogger().e("size: "+typedArray.getDimensionPixelSize(R.styleable.ImageViewText_textSize, 0));
// textSize = textSize/3;
String text = typedArray.getString(R.styleable.ImageViewText_text);
float textMarginLeft = typedArray.getDimension(R.styleable.ImageViewText_textMargin, 10);
float image_width = typedArray.getDimension(R.styleable.ImageViewText_image_width, 10);
float image_height = typedArray.getDimension(R.styleable.ImageViewText_image_height, 10);
int textColor = typedArray.getColor(R.styleable.ImageViewText_textColor, Color.TRANSPARENT);
if(textColor!=Color.TRANSPARENT){
mTextView.setTextColor(textColor);
}else {
int pressed = typedArray.getColor(R.styleable.ImageViewText_state_pressed, Color.BLACK);
int normal = typedArray.getColor(R.styleable.ImageViewText_state_normal, Color.BLACK);
int selected = typedArray.getColor(R.styleable.ImageViewText_state_selected, Color.BLACK);
mTextView.setTextColor(createColorStateList(normal,pressed,selected));
}
int background = typedArray.getResourceId(R.styleable.ImageViewText_view_background, 0);
int image_src = typedArray.getResourceId(R.styleable.ImageViewText_image_src, 0);
if (image_src!=0) {
mImageView.setBackgroundResource(image_src);
}
if (background!=0) {
view.setBackgroundResource(background);
}
String text_direction = typedArray.getString(R.styleable.ImageViewText_text_direction);
mTextView.setText(text);
mTextView.setTextSize(textSize);
LayoutParams imageLayoutParams = new LayoutParams((int) image_width,
(int) image_height);
Logger.getLogger().e("size: "+textSize+" w: "+image_width+" h: "+image_height );
Logger.getLogger().e("size: "+textSize+" w: "+DensityUtil.dip2px(context, image_width)+" h: "+DensityUtil.dip2px(context, image_height) );
mImageView.setLayoutParams(imageLayoutParams);
typedArray.recycle();//
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
if (TextDirection.LEFT.equals(text_direction)) {
this.setOrientation(HORIZONTAL);
params.rightMargin = (int) textMarginLeft;
mTextView.setLayoutParams(params);
addView(mTextView);
addView(mImageView);
} else if (TextDirection.RIGHT.equals(text_direction)) {
this.setOrientation(HORIZONTAL);
params.leftMargin = (int) textMarginLeft;
mTextView.setLayoutParams(params);
addView(mImageView);
addView(mTextView);
} else if (TextDirection.TOP.equals(text_direction)) {
this.setOrientation(VERTICAL);
params.bottomMargin = (int) textMarginLeft;
mTextView.setLayoutParams(params);
addView(mTextView);
addView(mImageView);
} else if (TextDirection.BOTTOM.equals(text_direction)) {
this.setOrientation(VERTICAL);
params.topMargin = (int) textMarginLeft;
mTextView.setLayoutParams(params);
addView(mImageView);
addView(mTextView);
}
}
public OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(v);
}
}
};
public OnClickListenerView listener;
public void setOnClickListener(OnClickListenerView listenerView) {
this.listener = listenerView;
}
public interface OnClickListenerView {
public void onClick(View v);
}
/** 对TextView设置不同状态时其文字颜色。 */
private ColorStateList createColorStateList(int normal, int pressed,
int selected) {
int[] colors = new int[] { pressed, selected, normal };
int[][] states = new int[3][];
states[0] = new int[] { android.R.attr.state_pressed,
android.R.attr.state_enabled };
states[1] = new int[] { android.R.attr.selectable,
android.R.attr.state_focused };
states[2] = new int[] {};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
}
public class TextDirection {
public static final String LEFT = "left";
public static final String TOP = "top";
public static final String RIGHT = "right";
public static final String BOTTOM = "bottom";
}
}
2、采用后台代码和布局文件:
注意属性duplicateParentState的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/me_app_selector"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="15dip" > <ImageView
android:id="@+id/iv_button_icon"
android:layout_width="35dip"
android:layout_height="35dip"
android:src="@drawable/me_settings" /> <TextView
android:id="@+id/tv_button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:duplicateParentState="true"
android:text="收藏"
android:textColor="@color/blue_shallow"
android:textSize="@dimen/font_body_16" /> </LinearLayout>
public class ImageViewText extends LinearLayout {
private LayoutInflater inflater;
private ImageView mImageView;
private TextView mTextView;
private View view;
public ImageViewText(Context context) {
super(context);
initView(context, null);
}
public ImageViewText(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.widget_imageview_text, null);
mImageView = (ImageView) view.findViewById(R.id.iv_button_icon);
mTextView = (TextView) view.findViewById(R.id.tv_button_text);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(ocl);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ImageViewText);
String text = a.getString(R.styleable.ImageViewText_text);
int drawable = a.getResourceId(R.styleable.ImageViewText_image_src, 0);
mTextView.setText(text);
mImageView.setImageResource(drawable);
a.recycle();
addView(view, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
public OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View v) {
if (listener!=null) {
listener.onClick(v);
}
}
};
//
// /**
// * 设置颜色
// */
// public OnTouchListener otl = new OnTouchListener() {
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// if (event.getAction() == MotionEvent.ACTION_DOWN) {
// view.setBackgroundColor(context.getResources().getColor(
// R.color.blue_shallow));
// } else if (event.getAction() == MotionEvent.ACTION_UP) {
// view.setBackgroundColor(Color.GRAY);
// }
// return false;
// }
// };
public OnClickListenerView listener;
public void setOnClickListener(OnClickListenerView listenerView) {
this.listener = listenerView;
}
public interface OnClickListenerView {
public void onClick(View v);
}
}
自定义属性之LinearLayout ImageView TextView模拟图片文字按钮的更多相关文章
- Android TextView(同时显示图片+文字)
见上图:需要图片和文字 在一起 之前的做法是用两个控件组成 <LinearLayout> <ImageView /> <TextView /> </Linea ...
- 安卓使用TextView实现图片加文字说明
背景:通讯录列表,每个单元格显示头像+名字,且头像显示圆形 方案一:ImageView + TextView 方案二:只用TextView + drawableLeft 属性 <TextView ...
- Android TextView中有图片有文字混合排列
Android TextView中有图片有文字混合排列 1.使用html.fromHtml 2.新建ImageGetter 3.使用<img src>标签 demo: 1.设置文字 ((T ...
- Android TextView 嵌套图片及其点击,TextView 部分文字点击,文字多颜色
1. TextView 中嵌套图片的方法 TextView textView... textView.setText("..."); textView.append(Html.fr ...
- Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种
http://blog.csdn.net/yanzi1225627/article/details/8633872 第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLa ...
- 026 Android 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+读取内存空间、手机进程信息+常驻悬浮框
1.目标效果 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+常驻悬浮框 2.页面布局文件 (1)activity_process_manager.xml <?xml ...
- 用RelativeLayout布局可以在imageview中写上文字
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- UIButton的titleEdgeInsets属性和imageEdgeInsets属性实现图片文字按要求排列
button可以设置 titleEdgeInsets属性和 imageEdgeInsets属性来调整其image和label相对位置,具体参考http://stackoverflow.com/ques ...
- iOS UIButton 图片文字上下垂直布局 解决方案
实现如图所示效果: 这是一个UIButton,需要改变image和title相对位置. 解决如下: //设置文字偏移:向下偏移图片高度+向左偏移图片宽度 (偏移量是根据[图片]大小来的,这点是关键)b ...
随机推荐
- 改善Java程序的151个建议(1-4)
1.不要在常量和变量中出现易混淆的数字 个人感觉这条在于编程命名的规范性.代码除了给机器看,也要给人看.要写能够结构清晰,命名规范,让人看懂的代码. 字母l作为长整型标志时务必大写 L 2.莫让常量蜕 ...
- Java 集合类常用方法
Collection中的contains()方法和remove()方法. boolean contains(Object o);该方法是用来判断集合中是否包含某个元素,若包含,返回true,不包含返回 ...
- ecs CentOS 7 安装 mariadb
检查之前是否已经安装 rpm -qa | grep mariadb 如果已安装,卸载 yum remove mysql mysql-server mysql-libs compat-mysql51 开 ...
- TCP基础知识(一)简介与数据包
TCP详解(1):简介与数据包 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 应用层向TCP层发送用于网间传输 ...
- jquery弹窗居中-类似alert()
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- K2P断流问题
本帖最后由 yufei8051 于 2019-3-15 15:29 编辑 感谢 “zhc887”的指点,把这3个文件删除后(后面大神说不是删除是清除,我直接删掉貌似也正常,建议听大神的)从这两天的使用 ...
- 139.00.004 Git学习-远程仓库之Github
参考Github官方HelloWorld入门教程 "有了远程仓库,妈妈再也不用担心我的硬盘了."--Git点读机 本章开始介绍Git的杀手级功能之一(注意是之一,也就是后面还有之二 ...
- Opencv2.4.13与Visual Studio2013环境搭建配置教程
转载:http://www.jb51.net/article/108943.htm 一.安装包的下载与安装 Opencv可免费到官网上去下载,opencv是国外软件,在下载是由于受资源的限制,可能会出 ...
- maven升级遇到的疑惑
今天在解决一个问题的时候,由于需要修改maven的client包,按照之前的办法,修改完之后,没有修改版本号,而是直接修改client的代码,之后直接 mvn deploy -e 打包上去了,然后奇怪 ...
- GitHub教程(二) 删除已有仓库
通过GitHub教程(一)的阅读,我相信您对GitHub体系框架已经有了模模糊糊的了解.本节教程将继续介绍GitHub的操作---删除仓库. 作为GitHub的入门使用者,我们可能会建一些简单的仓库来 ...