您的位置:首页 > 移动开发 > Android开发

Android自定义控件----YluoTextView可以改变Drawable大小颜色的TextView

2016-07-27 20:26 477 查看
在目前的项目中,很多地方都要用TextView.然后要添加图片,就通过drawableTop这种方式来实现.但是,png的实际高度跟通过getIntrinsicWidth和getIntrinsicHeight获取到的高度是有一定的偏差的.所以为了实际效果只能通过代码修改了,一个还好.但是每一个都要去修改的话那么得用多少代码啊. 还有TextView在点击的时候也需要一个按下的效果,平常的话可以通过selector来实现的,但是同理,得写多少个xml文件呢? 平时textView按下的时候一般都是修改一下颜色或者透明度的,为了实现这个效果设计师要多做几套图来.工足量大很多.因此为了解决这些问题,我就写了YluoTextView,直接看效果吧:



第一张是原始的TextView,

后面两张是YluoTextView的效果.

使用代码如下:

// 普通的TextView的用法
<TextView
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/e5b"
android:text="跑步"
android:textColor="#ff959595"/>
// YluoTextView的用法
<com.example.yluotextview.YluoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:drawableTop="@drawable/e5b"
android:text="跑步"
android:textColor="#ff959595"
yluo:create_presseddawable="true"
yluo:drawable_height="21dp"
yluo:drawable_width="21dp"
yluo:pressed_textcolor="#64959595"
yluo:use_yluo="true" />

<com.example.yluotextview.YluoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="50dp"
android:drawableTop="@drawable/e5b"
android:text="跑步"
android:textColor="#ff959595"
yluo:create_presseddawable="true"
yluo:drawable_height="21dp"
yluo:drawable_width="21dp"
yluo:pressed_textcolor="@android:color/holo_red_dark"
yluo:use_yluo="true" />


用法是不是很简单呢,就需要添加几个设置就可以省下N多功夫了.

项目下载地址在我的github上:https://github.com/yingtianxun/YluoTextView

如果现在的功能还不能满足大家,大家可以自己去修改代码呢.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息