应用情形:1、给头像添加一个边框(不一定是圆角边框,此处顺带讲解圆角边框)

    2、其它涉及图像使用ImageView的情形

解:设置ImageView 带必然宽度的Padding,同时设置android:adjustViewBounds 为True

       <ImageView
           android:id="@+id/photo"

           android:layout_width="80dip"
           android:layout_height="82dip"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="10dip"
           android:adjustViewBounds="true"
           android:padding="2dip"
           android:background="@drawable/test_image" 
           android:src="@drawable/main_screen_photo"/>


       test_image.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?> 

<shape

    xmlns:android="http://schemas.android/apk/res/android"

    android:shape="rectangle"

    android:useLevel="false">

   <corners

     android:radius="5dp"

    />

 <gradient

     android:startColor="#CCCC00"

     android:endColor="#CCCC00"

     android:useLevel="false"

     android:type="linear"

     android:angle="90"

     >

   </gradient>

</shape>


效果图如下:

更多推荐

Android开发:ImageView 设置圆角边框