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 ...
随机推荐
- 【行为型】Memento模式
备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态. 备忘录模式旨在对象的 ...
- python中函数的默认参数陷阱问题
其实也不能说是陷阱,只是一个不容易注意到的地方,尤其是有其他java/c++类编程语言经验的人员,这里涉及到python的一个特点,所以笔者说是陷阱只是一个噱头而已. def test(item, b ...
- The app icon set named "AppIcon" did not have any applicable content.
Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I a ...
- 转:Netty系列之Netty高性能之道
1. 背景 1.1. 惊人的性能数据 最近一个圈内朋友通过私信告诉我,通过使用Netty4 + Thrift压缩二进制编解码技术,他们实现了10W TPS(1K的复杂POJO对象)的跨节点远程服务调用 ...
- Regularized Linear Regression with scikit-learn
Regularized Linear Regression with scikit-learn Earlier we covered Ordinary Least Squares regression ...
- hdu 4550 卡片游戏
http://acm.hdu.edu.cn/showproblem.php?pid=4550 贪心 #include <cstdio> #include <cstring> # ...
- 桌面小部件----LED电子时钟实现
桌面控件是通过 Broadcast 的形式来进行控制的,因此每个桌面控件都对应于一个BroadcastReceiver.为了简化桌面控件的开发,Android 系统提供了一个 AppWidgetPro ...
- Convert Sorted Array to Binary Search Tree——LeetCode
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...
- POJ 2718 穷举
题意:给定一组数字,如0, 1, 2, 4, 6, 7,用这些数字组成两个数,并使这两个数之差最小.求这个最小差.在这个例子上,就是204和176,差为28. 分析:首先可以想到,这两个数必定是用各一 ...
- 【笔试&面试】C#的托管代码与非托管代码
1. C#中的托管代码是什么? 答:托管代码(ManagedCode)实际上就是中间语言(IL)代码.代码编写完毕后进行编译,此时编译器把代码编译成中间语言(IL),而不是能直接在你的电脑上运行的机器 ...