直接在layout文件夹下面新建一个.xml文件,然后在布局文件中就可以用啦~
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="30dip"/>
<solid android:color="#ccbbaa"/>
<stroke android:color="#ffffff"
android:width="4dp"
android:dashWidth="10dp"
android:dashGap="3dp"/>
<size
android:width="60dp"
android:height="60dp"/>
</shape>
画出来的小球长这个样子:

例:在一个相对布局中画一个小球:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/view01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/circle"
/>
<TextView
android:id="@+id/view02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/view01"
android:layout_alignLeft="@id/view01"
android:background="@drawable/circle"
/>
<TextView
android:id="@+id/view03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view01"
android:layout_alignLeft="@id/view01"
android:background="@drawable/circle"
/>
<TextView
android:id="@+id/view04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/view01"
android:layout_alignTop="@id/view01"
android:background="@drawable/circle"
/>
<TextView
android:id="@+id/view05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/view01"
android:layout_alignTop="@id/view01"
android:background="@drawable/circle"
/>
</RelativeLayout>
然后在MainActivity.java中使用就可以展示啦
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
}
}
效果如下:

本文介绍如何在Android应用中创建自定义圆形视图,并通过XML布局文件实现圆形及其边框样式。同时展示了如何在相对布局中排列多个圆形视图,以及如何在MainActivity中加载布局。
4234

被折叠的 条评论
为什么被折叠?



