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没有效果的更多相关文章

  1. android一个倾斜的TextView,适用于标签效果

    描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...

  2. 给TextView加上多彩效果:改变部分字体的大小和颜色

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/18363899 前言 在实际使用中,有时候会遇到特殊需求,比如pm突发奇想,想 ...

  3. SpannableString实现TextView的链接效果

    SpannableString实现TextView的链接效果 一.简介 TextView使用SpannableString设置复合文本TextView通常用来显示普通文本,但是有时候需要对其中某些文本 ...

  4. Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果

    一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...

  5. TextView跑马灯效果

    转载:http://www.2cto.com/kf/201409/330658.html 一.只想让TextView显示一行,但是文字超过TextView的长度怎么办?在开头显示省略号 android ...

  6. [Android1.5]TextView跑马灯效果

    from: http://www.cnblogs.com/over140/archive/2010/08/20/1804770.html 前言 这个效果在两周前搜索过,网上倒是有转载,可恨的是转载之后 ...

  7. Android学习总结——TextView跑马灯效果

    Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize="marquee" 2.TextView必须单行显示,即内容必须 ...

  8. 设置TextView的密码效果以及跑马灯效果

    密码效果以及跑马灯效果: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  9. android中设置TextView/Button 走马灯效果

    在Android的ApiDemo中,有Button的走马灯效果,但是换作是TextView,还是有一点差异. 定义走马灯(Marquee),主要在Project/res/layout/main.xml ...

随机推荐

  1. Python新手学习基础之数据结构-序列2

    长度.最大值和最小值 序列类型的数据结构,常常会用到长度检查.最大最小值检查的函数. 他们的作用: len(序列):返回列表的长度(元素个数): max(序列) :返回列表中元素最大值: min(序列 ...

  2. 线程间操作无效: 从不是创建控件“textBox2”的线程访问它

    如何:对 Windows 窗体控件进行线程安全调用 线程间操作无效: 从不是创建控件的线程访问它的三种方法 如果使用多线程处理来提高 Windows 窗体应用程序的性能,则你必须确保以线程安全的方式调 ...

  3. TIOBE.2017.01最新编程语言排行榜

    Jan 2017     Jan 2016     Change     Programming Language     Ratings     Change1    1        Java   ...

  4. Android中AsyncTask异步

    今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...

  5. 怎样导入SDWebImage

    Two ways : 方法1:copy all the files into your project. --下载https://github.com/rs/SDWebImage. --把SD项目co ...

  6. c语言之函数指针

    一.基础研究 这里研究的内容是函数指针,需要我们在研究后构造程序来描述函数指针数组的用法和向函数传函数指针的方法. 指针有很多种:整型指针.结构体指针.数组指针等等,它们的本质是它们的值都是一个地址, ...

  7. android.support.design.widget.AppBarLayout 在android5.0+底部显示空白条问题

    在最外层使用 RelativeLayout作为根节点,同时设置 android:fitsSystemWindows="true"问题解决. <?xml version=&qu ...

  8. mysql常用的命令大全

    常用的MySQL命令大全一.连接MySQL格式: mysql -h主机地址 -u用户名 -p用户密码1.例1:连接到本机上的MYSQL.首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令 ...

  9. 汇顶指纹传感器GF919深度解析

    前言: 随着指纹识别技术的日益普遍,其在手机上的应用也得到了广泛关注.作为全球第一款Android正面按压指纹识别手机,魅族MX4 Pro所搭载的国产指纹识别系统可谓是赚足了眼球,这就是由汇顶科技提供 ...

  10. 7.4.1 Dumping Data in SQL Format with mysqldump

    7.4 Using mysqldump for Backups 使用mysqldump 用于备份: 7.4.1 Dumping Data in SQL Format with mysqldump 7. ...