按道理來講,實(shí)現(xiàn)圓角很簡單,在shape中設(shè)置corners即可。但是有時(shí)候會(huì)出現(xiàn)即使設(shè)置了也不會(huì)出現(xiàn)圓角的效果。
屬性解釋
-
corners:設(shè)置圓角,只適用于rectangle類型,可分別設(shè)置四個(gè)角不同半徑的圓角。
android:radius 圓角半徑,會(huì)被下面每個(gè)特定的圓角屬性重寫
android:topLeftRadius 左上角的半徑
android:topRightRadius 右上角的半徑
android:bottomLeftRadius 左下角的半徑
android:bottomRightRadius 右下角的半徑
問題與解決
比如我想設(shè)置左邊兩個(gè)角為圓角,正常思路是:
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
/>
但實(shí)際情況是,這樣并不會(huì)出現(xiàn)圓角的效果。
正確的做法是:
<corners
android:radius="2dp" //這里要首先設(shè)置
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
/>
此時(shí),在Android studio 中看預(yù)覽效果還是4個(gè)圓角, 但實(shí)際運(yùn)行是滿足的。
- 注意以下幾點(diǎn)
1、在設(shè)置圓角時(shí),圓角半徑的大小必須大于1,否則是沒有圓角效果的。
2、如果你想單獨(dú)設(shè)置某幾個(gè)角是圓角, 你必須首先聲明 radius 屬性(必須大于1),
然后在其他四個(gè)角的屬性中設(shè)置每個(gè)角的實(shí)際想要的半徑大小, 不想圓角的設(shè)置為("0dp").