带图片(drawableXxx)的TextView
2015-12-20 17:12
288 查看
基本用法:
设置图片的核心其实就是:drawableXxx;可以设置四个方向的图片: drawableTop(上), drawableButtom(下), drawableLeft (左), drawableRight(右) 。另外,也可以使用drawablePadding来设置图片与文字间的间距!
效果图:
实现代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jay.example.test.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:drawableTop="@drawable/user_48" android:drawablePadding="4dp" android:gravity="center" android:text="用户" /> </RelativeLayout>
图片大小设置:
Drawable在XML中无法直接设置其大小,需要在Java代码中来进行修改。部分代码如下:
txt = (TextView) findViewById(R.id.txt); Drawable[] drawable = txt.getCompoundDrawables(); // 数组下表0~3,依次是:左上右下 drawable[0].setBounds(100, 0, 200, 200); txt.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);
代码分析:
1.Drawable[] drawable = txt.getCompoundDrawables( ); 获得四个不同方向上的图片资源,数组元素依次是:左上右下的图。
2.drawable[1].setBounds(100, 0, 200, 200); 接着获得资源后,可以调用setBounds设置左上右下坐标点,比如这里设置了代表的是: 长是:从离文字最左边开始100dp处到200dp处 宽是:从文字上方0dp处往上延伸200dp!
3.txt.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);为TextView重新设置drawable数组!没有图片可以用null代替哦! PS:另外,从上面看出我们也可以直接在Java代码中调用setCompoundDrawables为 TextView设置图片!
相关文章推荐
- 每天学点Python之数值类型
- codevs 4438 YJQ runs upstairs
- UVM训练场
- 怎么用secureCRT连接Linux
- C的经典算法
- 对有源滤波器和三极管的一些理解
- RecylerView通用分隔线实现
- html5的特性
- 造轮子_MVC+EF(CodeFirst)+Easyui医药MIS系统
- VC6.0和相应的插件安装
- 【APUE】7、signal函数
- 每天学点Python之list
- 经济--票据
- python sort函数
- RAID
- org.hibernate.TransientObjectException: The given object has a null identifier;
- posix thread内存可视性
- 20135232期末总结
- html5的改进与沿革
- WordPress添加个性化的博客宠物的方法