View控件中android:drawablePadding不起作用的原因探究
2015-07-16 16:47
525 查看
我们在使用android:drawablePadding的时候,经常会出现设置了以后会不起作用,先来看一下代码:
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="300dp"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center"
android:drawablePadding="10dp"
android:text="ooooo" />
</LinearLayout>
我们设置了一下android:drawablePadding为10dp,我们来看下效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/fd0c932905feefd32055b55812d479eb)
我们看到,这个图片和文本之间的距离还是这么大,肯定不是我们设置的10dp,难道是这个属性不起作用,经过多次试验发现,我们的控件大小设置为wrap_content,这个android:drawablePadding才起作用,现在改了以后看下效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/9e3de5672ec0eeba155fb29227dfbf24)
现在有效果了,如果控件的大小设置为wrap_content,默认文本和图像的大小为0,如果设置为match_parent或者固定的高度,则图片会尽量往外靠,android:drawablePadding会不起作用,所以大家在使用android:drawablePadding这个属性的时候要注意这个问题!!!
转载自:http://blog.csdn.net/lovexieyuan520/article/details/43021725
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="300dp"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center"
android:drawablePadding="10dp"
android:text="ooooo" />
</LinearLayout>
我们设置了一下android:drawablePadding为10dp,我们来看下效果:
我们看到,这个图片和文本之间的距离还是这么大,肯定不是我们设置的10dp,难道是这个属性不起作用,经过多次试验发现,我们的控件大小设置为wrap_content,这个android:drawablePadding才起作用,现在改了以后看下效果:
现在有效果了,如果控件的大小设置为wrap_content,默认文本和图像的大小为0,如果设置为match_parent或者固定的高度,则图片会尽量往外靠,android:drawablePadding会不起作用,所以大家在使用android:drawablePadding这个属性的时候要注意这个问题!!!
转载自:http://blog.csdn.net/lovexieyuan520/article/details/43021725
相关文章推荐
- Android 屏幕适配方案
- Android自定义View——自定义搜索框(SearchView)
- 关于 android 基础知识的总结 之 定时器
- Android中截取照片
- 【Android测试】【第三节】ADB——源码浅谈
- android dp与px之间转换以及px与sp之间的转换
- android 设备在设置节能模式的情况下 调用 Toast 会 唤醒屏幕 突然变亮
- Android Fragment 你应该知道的一切
- Android下如何把屏幕录制演示生成gif动态图
- Android之Service与Activity通信机制
- Android地图 获取经纬度计算距离介绍
- Android 特别大的Activity和Fragment的生命周期图
- Android使用TextView,设置onClick属性无效解决办法
- Android反编译工具使用
- html5模拟Android下拉刷新
- Android---利用Google的云消息
- Android-studio第一次开始跳过SDK下载
- Android触控基础:MotionEvent
- Android通过反射打造可以存储任何对象的万能SharedPreferences
- Windows环境下Android Studio安装教程