安卓自带控件中几乎没有圆形控件,但在实际开发过程中圆形控件更能给用户更好的视觉效果。
先看一下我们要实现的效果:
要实现这种效果的话先要在drawable文件夹下新建一个Android XML文件,我们起名为circle。代码如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="100dp"
android:height="100dp"/>
<stroke android:width="1dp"
android:color="#929292"/>
<solid android:color="#FFFFFF"/>
</shape>
其中shape属性设置为oval,表示为椭圆形,然后在size中设置宽高,宽高相同,表示圆形
stroke主要用来设置边框颜色和边框的宽度
solide主要用于设置圆内的颜色
然后我们该写布局文件了
<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=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/accountpager_pen_icon"
android:background="@drawable/cricle"
/>
</RelativeLayout>
在布局文件中添加了一个Imageview控件,居中放置,在background中引用我们刚才写的那个xml文件
这时候圆会出现,我们可以在圆中添加一个图片,我在里面添加了一个笔的图片,是在src中把图片引入进去的
我们在添加图片的时候可能要注意了,如果我们添加的图片过大,可能这时的圆会变形,我们可以通过修改我们写的那个cricle文件的长宽,也可以修改图片的大小