1.当TextView 设置宽度设置为match_parent的时候

TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远

2.当TextView 设置的宽度为wrap_parent的时候,extView drawablePadding有效果

解决:自定义TextView

  1. package com.charlie.chpro.customview;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.drawable.Drawable;
  6. import android.util.AttributeSet;
  7. import android.widget.TextView;
  8.  
  9. import com.charlie.chpro.R;
  10.  
  11. /**
  12. * 设置TextView Drawable的大小
  13. * Created by Charlie on 2016/7/30.
  14. */
  15.  
  16. public class ResetDrawableSizeTextView extends TextView {
  17. // 需要从xml中读取的各个方向图片的宽和高
  18. private int leftHeight = -1;
  19. private int leftWidth = -1;
  20. private int rightHeight = -1;
  21. private int rightWidth = -1;
  22. private int topHeight = -1;
  23. private int topWidth = -1;
  24. private int bottomHeight = -1;
  25. private int bottomWidth = -1;
  26.  
  27. public ResetDrawableSizeTextView(Context context) {
  28. super(context);
  29. }
  30.  
  31. public ResetDrawableSizeTextView(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. // super一定要在我们的代码之前配置文件
  34. init(context, attrs, 0);
  35. }
  36.  
  37. public ResetDrawableSizeTextView(Context context, AttributeSet attrs, int defStyle) {
  38. super(context, attrs, defStyle);
  39. // super一定要在我们的代码之前配置文件
  40. init(context, attrs, defStyle);
  41. }
  42.  
  43. /**
  44. * 初始化读取参数
  45. * */
  46. private void init(Context context, AttributeSet attrs, int defStyle) {
  47. // TypeArray中含有我们需要使用的参数
  48. TypedArray a = context.obtainStyledAttributes(attrs,
  49. R.styleable.ResetDrawableSizeTextView, defStyle, 0);
  50. if (a != null) {
  51. // 获得参数个数
  52. int count = a.getIndexCount();
  53. int index = 0;
  54. // 遍历参数。先将index从TypedArray中读出来,
  55. // 得到的这个index对应于attrs.xml中设置的参数名称在R中编译得到的数
  56. // 这里会得到各个方向的宽和高
  57. for (int i = 0; i < count; i++) {
  58. index = a.getIndex(i);
  59. switch (index) {
  60. case R.styleable.ResetDrawableSizeTextView_bottom_height:
  61. bottomHeight = a.getDimensionPixelSize(index, -1);
  62. break;
  63. case R.styleable.ResetDrawableSizeTextView_bottom_width:
  64. bottomWidth = a.getDimensionPixelSize(index, -1);
  65. break;
  66. case R.styleable.ResetDrawableSizeTextView_left_height:
  67. leftHeight = a.getDimensionPixelSize(index, -1);
  68. break;
  69. case R.styleable.ResetDrawableSizeTextView_left_width:
  70. leftWidth = a.getDimensionPixelSize(index, -1);
  71. break;
  72. case R.styleable.ResetDrawableSizeTextView_right_height:
  73. rightHeight = a.getDimensionPixelSize(index, -1);
  74. break;
  75. case R.styleable.ResetDrawableSizeTextView_right_width:
  76. rightWidth = a.getDimensionPixelSize(index, -1);
  77. break;
  78. case R.styleable.ResetDrawableSizeTextView_top_height:
  79. topHeight = a.getDimensionPixelSize(index, -1);
  80. break;
  81. case R.styleable.ResetDrawableSizeTextView_top_width:
  82. topWidth = a.getDimensionPixelSize(index, -1);
  83. break;
  84. }
  85. }
  86.  
  87. // 获取各个方向的图片,按照:左-上-右-下 的顺序存于数组中
  88. Drawable[] drawables = getCompoundDrawables();
  89. int dir = 0;
  90. // 0-left; 1-top; 2-right; 3-bottom;
  91. for (Drawable drawable : drawables) {
  92. // 设定图片大小
  93. setImageSize(drawable, dir++);
  94. }
  95. // 将图片放回到TextView中
  96. setCompoundDrawables(drawables[0], drawables[1], drawables[2],
  97. drawables[3]);
  98.  
  99. }
  100.  
  101. }
  102.  
  103. /**
  104. * 设定图片的大小
  105. * */
  106. private void setImageSize(Drawable d, int dir) {
  107. if (d == null) {
  108. return;
  109. }
  110.  
  111. int height = -1;
  112. int width = -1;
  113. // 根据方向给宽和高赋值
  114. switch (dir) {
  115. case 0:
  116. // left
  117. height = leftHeight;
  118. width = leftWidth;
  119. break;
  120. case 1:
  121. // top
  122. height = topHeight;
  123. width = topWidth;
  124. break;
  125. case 2:
  126. // right
  127. height = rightHeight;
  128. width = rightWidth;
  129. break;
  130. case 3:
  131. // bottom
  132. height = bottomHeight;
  133. width = bottomWidth;
  134. break;
  135. }
  136. // 如果有某个方向的宽或者高没有设定值,则不去设定图片大小
  137. if (width != -1 && height != -1) {
  138. d.setBounds(0, 0, width, height);
  139. }
  140. }
  141. }

xml里面使用

  1. <com.charlie.chpro.customview.ResetDrawableSizeTextView
  2. android:id="@+id/ctv_left_title"
  3. style="@style/back_toolbar_textview_style"
  4. android:layout_gravity="left"
  5. android:layout_marginLeft="@dimen/x18"
  6. android:drawableLeft="@mipmap/back_normal"
  7. android:drawablePadding="@dimen/x6"
  8. android:text="@string/back"
  9. android:textSize="@dimen/y32"
  10. app:left_height="@dimen/y42"
  11. app:left_width="@dimen/x24"
  12. />

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. [解决]UserLibrary中的jar包不会自动发布Tomcat的lib目录下(基于MyEclipse2014)

    1.在工程名称上单击[右键] —— 单击[Properties]选项,点击后会弹出属性窗口: 2.选择[Properties]后在左侧树中找到[MyEclipse] —— [Deployment As ...

  2. 简单选择排序(Simple Selection Sort)的C语言实现

    简单选择排序(Simple Selection Sort)的核心思想是每次选择无序序列最小的数放在有序序列最后 演示实例: C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp) 原创文章, ...

  3. 使用Gson进行json数据转换(list to json 和json to list)

    文章借鉴自:http://blog.csdn.net/binyao02123202/article/details/7540407 下面是一个简单的例子: Java代码 public class Pe ...

  4. c语言实现一个链表

    一.基础研究 我们在这里要理解和实现一种最基本的数据结构:链表.首先看看实现的程序代码: List .h: 事实上我们观察list.h发现前面一部分是数据结构的定义和函数的声明,后面一部分是函数的实现 ...

  5. html5 本地存储

    < ![CDATA[ 1. html本地存储操作 首先引用 <script src="Scripts/jquery-2.0.0.js"></script&g ...

  6. 【Oracle】Windows 7下完全卸载Oracle 11g数据库

    闲来无事,想把Oracle 11g重装一下,记录如下:   (1)首先在服务中停止所有的Oracle服务:   (2)开始 -> 程序 -> Oracle-OraDb11g_home1 - ...

  7. 【Oracle】安装

    http://www.2cto.com/database/201208/150620.html 呵呵,花了一个多小时,左右把11g安装折腾好了.其中折腾SQL Developer 花了好长时间,总算搞 ...

  8. SQL Standard Based Hive Authorization(基于SQL标准的Hive授权)

    说明:该文档翻译/整理于Hive官方文档https://cwiki.apache.org/confluence/display/Hive/SQL+Standard+Based+Hive+Authori ...

  9. HDOJ 2206 IP的计算(正则表达式的应用)

    Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...

  10. Emacs学习阶段小结-Emacs常用快捷键总结

    首先推荐一下Emacs自带的Emacs Tutorial,跟着这个做一边,两三个小时,基本的使用就能掌握了.之后的神教程就有很多了,比如<Sams.Teach.Yourself.Emacs.in ...