TextView drawablePadding没有效果
1.当TextView 设置宽度设置为match_parent的时候
TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远
2.当TextView 设置的宽度为wrap_parent的时候,extView drawablePadding有效果
解决:自定义TextView
package com.charlie.chpro.customview; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView; import com.charlie.chpro.R; /**
* 设置TextView Drawable的大小
* Created by Charlie on 2016/7/30.
*/ public class ResetDrawableSizeTextView extends TextView {
// 需要从xml中读取的各个方向图片的宽和高
private int leftHeight = -1;
private int leftWidth = -1;
private int rightHeight = -1;
private int rightWidth = -1;
private int topHeight = -1;
private int topWidth = -1;
private int bottomHeight = -1;
private int bottomWidth = -1; public ResetDrawableSizeTextView(Context context) {
super(context);
} public ResetDrawableSizeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// super一定要在我们的代码之前配置文件
init(context, attrs, 0);
} public ResetDrawableSizeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// super一定要在我们的代码之前配置文件
init(context, attrs, defStyle);
} /**
* 初始化读取参数
* */
private void init(Context context, AttributeSet attrs, int defStyle) {
// TypeArray中含有我们需要使用的参数
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ResetDrawableSizeTextView, defStyle, 0);
if (a != null) {
// 获得参数个数
int count = a.getIndexCount();
int index = 0;
// 遍历参数。先将index从TypedArray中读出来,
// 得到的这个index对应于attrs.xml中设置的参数名称在R中编译得到的数
// 这里会得到各个方向的宽和高
for (int i = 0; i < count; i++) {
index = a.getIndex(i);
switch (index) {
case R.styleable.ResetDrawableSizeTextView_bottom_height:
bottomHeight = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_bottom_width:
bottomWidth = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_left_height:
leftHeight = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_left_width:
leftWidth = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_right_height:
rightHeight = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_right_width:
rightWidth = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_top_height:
topHeight = a.getDimensionPixelSize(index, -1);
break;
case R.styleable.ResetDrawableSizeTextView_top_width:
topWidth = a.getDimensionPixelSize(index, -1);
break;
}
} // 获取各个方向的图片,按照:左-上-右-下 的顺序存于数组中
Drawable[] drawables = getCompoundDrawables();
int dir = 0;
// 0-left; 1-top; 2-right; 3-bottom;
for (Drawable drawable : drawables) {
// 设定图片大小
setImageSize(drawable, dir++);
}
// 将图片放回到TextView中
setCompoundDrawables(drawables[0], drawables[1], drawables[2],
drawables[3]); } } /**
* 设定图片的大小
* */
private void setImageSize(Drawable d, int dir) {
if (d == null) {
return;
} int height = -1;
int width = -1;
// 根据方向给宽和高赋值
switch (dir) {
case 0:
// left
height = leftHeight;
width = leftWidth;
break;
case 1:
// top
height = topHeight;
width = topWidth;
break;
case 2:
// right
height = rightHeight;
width = rightWidth;
break;
case 3:
// bottom
height = bottomHeight;
width = bottomWidth;
break;
}
// 如果有某个方向的宽或者高没有设定值,则不去设定图片大小
if (width != -1 && height != -1) {
d.setBounds(0, 0, width, height);
}
}
}
xml里面使用
<com.charlie.chpro.customview.ResetDrawableSizeTextView
android:id="@+id/ctv_left_title"
style="@style/back_toolbar_textview_style"
android:layout_gravity="left"
android:layout_marginLeft="@dimen/x18"
android:drawableLeft="@mipmap/back_normal"
android:drawablePadding="@dimen/x6"
android:text="@string/back"
android:textSize="@dimen/y32"
app:left_height="@dimen/y42"
app:left_width="@dimen/x24"
/>
TextView drawablePadding没有效果的更多相关文章
- android一个倾斜的TextView,适用于标签效果
描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...
- 给TextView加上多彩效果:改变部分字体的大小和颜色
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/18363899 前言 在实际使用中,有时候会遇到特殊需求,比如pm突发奇想,想 ...
- SpannableString实现TextView的链接效果
SpannableString实现TextView的链接效果 一.简介 TextView使用SpannableString设置复合文本TextView通常用来显示普通文本,但是有时候需要对其中某些文本 ...
- Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果
一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...
- TextView跑马灯效果
转载:http://www.2cto.com/kf/201409/330658.html 一.只想让TextView显示一行,但是文字超过TextView的长度怎么办?在开头显示省略号 android ...
- [Android1.5]TextView跑马灯效果
from: http://www.cnblogs.com/over140/archive/2010/08/20/1804770.html 前言 这个效果在两周前搜索过,网上倒是有转载,可恨的是转载之后 ...
- Android学习总结——TextView跑马灯效果
Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize="marquee" 2.TextView必须单行显示,即内容必须 ...
- 设置TextView的密码效果以及跑马灯效果
密码效果以及跑马灯效果: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- android中设置TextView/Button 走马灯效果
在Android的ApiDemo中,有Button的走马灯效果,但是换作是TextView,还是有一点差异. 定义走马灯(Marquee),主要在Project/res/layout/main.xml ...
随机推荐
- linux c最简单的加密程序
最初的密码程序是在Hirst First c里面看到的,大概内容如下:对待加密的字符串的每一个字符和某个数值进行一次按位异或得到密文,再进行一次按位异或得到明文. 补充知识:按位异或的结果是“同位得1 ...
- 机器学习(4)之Logistic回归
机器学习(4)之Logistic回归 1. 算法推导 与之前学过的梯度下降等不同,Logistic回归是一类分类问题,而前者是回归问题.回归问题中,尝试预测的变量y是连续的变量,而在分类问题中,y是一 ...
- 手机控制电脑,在WIFI局域网下(关机,重启,遥控)
这个软件叫百变遥控:http://blog.sina.com.cn/s/blog_9abc7dbc0101hmut.html 今天周末,在家里看电影,家里用的是台式电脑,我自己买了一个投影仪来专门看视 ...
- 利用Tree命令生成磁盘文件列表
命令原型:D:/>tree /? 以图形显示驱动器或路径的文件夹结构.TREE [drive:][path] [/F] [/A]/F 显示每个文件夹中文件的名称./A 使用 ASCII 字符,而 ...
- GitHub上最火的开源项目SlidingMenu导入出错的终极解决方案
SlidingMenu 开源项目下载地址 : https://github.com/jfeinstein10/slidingmenu Actionbarsherlock 开源项目下载地址 :http: ...
- ext2,ext3,ext4文件系统管理软件包e2fsprogs
e2fsprogs(也叫做e2fs programs)是一个Ext2(及Ext3/4)文件系统工具集(Ext2 Filesystems Utilities[2] ),它包含了诸如创建.修复.配置.调试 ...
- Miller_Rabin素数判断,rho
safe保险一点5吧.我是MR: ; int gcd(int a,int b){return !b?a:gcd(b,a%b);} int mul(int a,int b,int p){ )*p); ? ...
- fmt命令
简单的格式化文本 fmt [option] [file-list] fmt通过将所有非空白行的长度设置为几乎相同,来进行简单的文本格式化 参数 fmt从file-list中读取文件,并将其内容的格式化 ...
- org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.
No action config found for the specified url url路径下找不到action,原因是stuts-config.xml文件配置错误. demo的项目文件如下: ...
- int.Parse()与int.TryParse()
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...