如何在代码中动态为TextView/Button设置drawableRight
2015-09-22 20:35
387 查看
在写TextView的这个属性的drawableRight时,一般都是在xml里面写好的,但是我们有时候的需求是想在代码中动态的添加的效果,那么接下来我就来说说具体的方法。
工具/原料
· android adt eclipse
· avd genymotion
方法/步骤
1. <TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/check_down"
android:gravity="center_vertical"
android:textSize="24dip"
android:maxLines="1"
android:ellipsize="end"/>
我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法
2. public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
这个方法呢,就是可以在Java代码动态的画 左上右下几个方向
类似于xml中的 android:drawableLeft="@drawable/icon"
android:drawableTop="@drawable/icon"
android:drawableRight="@drawable/icon"
android:drawableButtom="@drawable/icon"
3. 具体在代码中的用法是:
Drawable drawable =getResources().getDrawable(R.drawable.spinner_checked);
drawable.setBounds(0, 0,drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
titleTv.setCompoundDrawables(null, null,drawable, null);//画在右边
4. 运行之后的效果
5. 5
界面比较丑,勿喷,大家也还可以通过这个方法实现
public voidsetCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top, Drawable right, Drawablebottom)
工具/原料
· android adt eclipse
· avd genymotion
方法/步骤
1. <TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/check_down"
android:gravity="center_vertical"
android:textSize="24dip"
android:maxLines="1"
android:ellipsize="end"/>
我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法
2. public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
这个方法呢,就是可以在Java代码动态的画 左上右下几个方向
类似于xml中的 android:drawableLeft="@drawable/icon"
android:drawableTop="@drawable/icon"
android:drawableRight="@drawable/icon"
android:drawableButtom="@drawable/icon"
3. 具体在代码中的用法是:
Drawable drawable =getResources().getDrawable(R.drawable.spinner_checked);
drawable.setBounds(0, 0,drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
titleTv.setCompoundDrawables(null, null,drawable, null);//画在右边
4. 运行之后的效果
5. 5
界面比较丑,勿喷,大家也还可以通过这个方法实现
public voidsetCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top, Drawable right, Drawablebottom)
相关文章推荐
- 关于FFTW3和NE10源代码库的编译
- map初次使用
- web.xml中的contextConfigLocation在spring中的作用
- JNI
- Java对map进行排序(包括按键和值)
- gbk字符串长度
- 破晓系列1---java初体验、跨平台、安装、环境变量配置、cmd基本命令、第一个helloworld
- C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- unix编程实训教程之 more(觉醒篇一 Begin)
- Java中HashMap源码解析
- JAVA问题总结之22--Arraylist集合的操作
- C++学习记录之string字符串
- java第四周作业——3.22
- C语言程序初体验-第六课-第三题:一周工资数
- <问题>Eclipse中Deploy应用到GAE的错误
- C# list 多条件排序
- 使用百度地图SDK来开发自己的程序(主要是考察自己读代码的能力)
- Windows环境下修改Eclipse随笔提示
- JAVA Game: picking a card
- pkcs7 python ecb ebe