1.创建shape

2定义椭圆
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--指定形状椭圆-->
<!--指定形状内部颜色-->
<solid android:color="#ff66aa">
</solid>
<!--指定形状轮廓的粗细与颜色-->
<stroke android:width="1dp"
android:color="#aaaaaa">
</stroke>
</shape>

3.定义圆角矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--指定形状内部颜色-->
<solid android:color="#ffdd66">
</solid>
<!--指定形状轮廓的粗细与颜色-->
<stroke android:width="1dp"
android:color="#aaaaaa">
</stroke>
<!--指定形状四个圆角半径-->
<corners android:radius="10dp">
</corners>
</shape>

3.页面切换背景图形
<View
android:id="@+id/v_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="圆角矩形">
</Button>
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="椭圆形">
</Button>
</LinearLayout>
//点击按钮,切换背景 view = findViewById(R.id.v_content); findViewById(R.id.btn4).setOnClickListener(this::onClick); findViewById(R.id.btn5).setOnClickListener(this::onClick); view.setBackgroundResource(R.drawable.shape_rect_gold);
private void onClick(View v) {
switch (v.getId()) {
case R.id.btn4:
view.setBackgroundResource(R.drawable.shape_rect_gold);
break;
case R.id.btn5:
view.setBackgroundResource(R.drawable.shape_oval_rose);
break;
}
}



















