shape文件圆角bug的一种解决方法

好吧,这可以说不是原创,只是总结而已。

最近项目中遇到一个问题,需要作出如下布局效果


这是两个按钮,按下时有颜色变化,本想用点九图片,但发现也不是很好布局,于是改用shape文件,其实此类扁平化的按钮效果,基本上都可以使用shape。

实现思路:

1.搞一个LinearLayout,水平的,给它设置android:background="@drawable/shapebg",其shapebg.xml 文件如下

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

    <solid android:color="#ffffff" />

    <corners android:radius="15dip" />

    <stroke
        android:width="1dip"
        android:color="#000000" />

</shape>

 

2.两边分别弄两个button(我这用textview代替),中间放一根竖线,具体代码如下:

<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" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dip"
        android:layout_marginLeft="50dip"
        android:background="@drawable/shapebg"
        android:padding="1dip" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="80dip"
            android:layout_height="50dip"
            android:background="@drawable/shapeleftlist"
            android:gravity="center"
            android:text="同意" />

        <View
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:background="#000000" />

        <TextView
            android:layout_width="80dip"
            android:layout_height="50dip"
            android:background="@drawable/shaperightlist"
            android:gravity="center"
            android:text="拒绝" />
    </LinearLayout>

</RelativeLayout>
3.两边的textview分别设置圆角即实现如下效果


代码文件为:

<TextView
        android:id="@+id/textView2"
        android:layout_width="70dip"
        android:layout_height="40dip"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="39dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:background="@drawable/yj"
        android:text="TextView" />
shape文件为
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners
        android:topLeftRadius="10dip"
        android:topRightRadius="0dip" 
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="0dip"
       />

    <solid android:color="#456745" />

</shape>

经测试在android 3.0以下版本中,android:bottomLeftRadius,android:bottomRightRadius互换了,变成了对角的效果,如图


于是查找资料得知,此乃低版本bug,3.0以上版本已修复。但如何兼容呢,据说可以创建一个drawable-v14文件夹,此文件系统运行时高版本优先引用,正确的代码可以放到此文件夹中,其错误的文件即android:bottomLeftRadius,android:bottomRightRadius互换的文件可以放到正常的文件夹中。这种方法我没有试过,不知可不可行,然而这样总觉怪异。再次百度,发现可以使用layer-list绘制两层shape,叠加得到以上效果,这样可以避免左右下角互换的问题,代码如下

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="15dip" />
            <solid android:color="#00000000"/>
        </shape>
    </item>
    <item android:left="15dip">
        <shape android:shape="rectangle" >
            <solid android:color="#00000000"/>
        </shape>
    </item>
</layer-list>
其原理是,先绘制一层全圆角的矩形,再在其上层覆盖一层无圆角矩形,偏移一定距离,遮盖掉右边的圆角,如此高低版本表现就一致了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值