您的位置:首页 > 移动开发 > Android开发

Android中shape的使用

2017-10-27 11:07 393 查看
PS:通常情况下,我们在写安卓程序的时候,会用到表格,比如说,需要一个上左右角是有弧度,下左右角没有弧度的一个弹出框,又或者是需要把一个图片用给一个表格框起来来等,这些功能shape都可以做到。但是这里面有坑,我之前用的androidstudio2.2,创建shape文件的时候默认是api23及以上的,所有属性都能正常使用,但是今天我换到studio1.5的时候,选的api是19,里面的圆角属性一直不能同时使用,换到22还是不行,我一直以为是版本问题,就把studio升级到了2.0作为测试一下,但还是不行,巧的是换到23就可以了,我不知道其他小伙伴是不是出现过这种情况,如果有的话,就照我这个方法试一下,如果没有,那就学一下shape属性的使用也是不错的哟。

如代码

<corners android:bottomLeftRadius="30dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="30dp"
android:topRightRadius="0dp"></corners>
API23之前效果一直是这样的



换到API23后就是这样的了:



切换API:



所以建议大家使用的时候多切换一下API,怕是API影响了效果,如果有什么问题,或者我说错了,大家可以给我留言,互相探讨一下

1:画边框

划表格框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

//表格的边框
<stroke
android:width="1dp"
android:color="#000000" />
//内部的颜色
<gradient
android:angle="225"
android:endColor="#CCE8CF"
android:startColor="#CCE8CF" />

</shape
对于边框stroke,可以控制边框的样式,比如说是dash破折号,破折号间隔13dp,宽度10dp.
<stroke android:color="@color/colorAccent" android:width="2dp"
android:dashGap="13dp" android:dashWidth="10dp">

</stroke>



2:一头有弧度
4000
而另一头没有弧度

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
//角落
<corners
android:topLeftRadius="15dp"
android:bottomLeftRadius="15dip"
android:topRightRadius="0dip"
android:bottomRightRadius="0dip"
/>
//里面填充的颜色
<solid
android:color="#666666"
/>
</shape>



3:里面是一张图片,外面是一个边框(类似于表格)

或者是两边有边线,中间是空的(白色或者透明),原理是叠层,下面的item把上面的item盖住了。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is the main color -->
<item >
<shape>
<solid android:color="#000000" />

</shape>
</item>
<!-- This is the line -->
<item
android:left="2dp"
android:right="4dp"
android:top="2dp"
android:bottom="2dp"
android:drawable="@drawable/a"
>

</item>
</layer-list>


       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: