前言
每次我添加一个ImageView的时候,发现我设置的宽高和我想要的宽高存在出入,在研究之后发现实际上是因为图片还存在一定距离的内边距,导致我需要100x100的图片,看起来只有60x60。那么怎么样才能让我方便的看到这个图片实际上的大小呢,于是我想到,把图片加上边框不就行了
实现
drawable——新建一个资源文件当做背景图
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<!--圆角边框-->
<!--
<corners android:radius="15dp" />-->
<!--虚线-->
<!--<stroke android:width="1dp"
android:color="#000000"
android:dashWidth="5dp"
android:dashGap="2dp"
/>-->
<stroke android:width="1dp"
android:color="#000000"/>
<padding
android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp"
/>
</shape>
然后在需要设置为边框的图片上,把background引用到这个文件就可以了
示例
layout文件
...
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/level_fragment_shadow"
android:background="@drawable/test_border"
/>
...
实现图: