来自:http://www.dabu.info/android-shape-drawable-create-jian-liang-side-semicircle-backgound-button.html
Creating a rectangle shape with only two rounded edges
先上效果图:
第一种:
现在drawable/下创建semi_round_backgroud.xml:
semi_round_backgroud.xml:
| <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:color="@color/colorAccent"> <corners android:radius="60dip" /> <stroke android:width="0dp" android:color="@color/colorAccent" /> <solid android:color="@color/colorAccent" /> </shape> |
第二种:
semi2.xml:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/colorAccent"
/>
<size
android:height="30dp"
/>
<corners
android:bottomRightRadius="30dp"
android:bottomLeftRadius="30dp"
android:topRightRadius="30dp"
android:topLeftRadius="30dp"
/>
</shape>
|
然后是activity_main.xml引用上面创建的背景图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?xml
version="1.0"
encoding="utf-8"?>
<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">
<TextView
android:text="Hello
World!"
android:id="@+id/tv1"
android:background="@drawable/semi_round_backgroud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
<TextView
android:text="Hello
World!"
android:id="@+id/tv2"
android:background="@drawable/semi2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:layout_below="@+id/tv1"
android:layout_marginTop="15dp"/>
</RelativeLayout>
|
这里,我发现一个问题,那就是android studio的布局预览功能是有问题的,也就是预览可以,但是并不一定就是在真实手机上呈现的效果,比如,第一种和第二种方法,在android studio的预览效果如下:
上面是第一种,下面是第二种。肉眼可以看出,第二种要比第一种半圆要尖一些。
但是在真实的手机所呈现的效果却没有这种区别,真实手机上的效果截图:
是不是两种一样的,所以,这个例子也告诉我们,android studio的预览功能不可靠,确切的说,应该是谷歌提供的界面预览工具不可靠,建议看真是效果,一定还是要在实体机上测试。