rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="6dp"/>
<!-- 设置填充颜色 -->
<solid android:color="#fff"/>
<!-- 设置四周的内边距 -->
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp"/>
<!-- 设置边框 -->
<stroke android:width="3dp" android:color="#000"/>
</shape>
gradient_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义填充渐变颜色 -->
<gradient
android:startColor="#ff0"
android:endColor="#00f"
android:type="linear"/>
<!-- 设置圆角矩形 -->
<corners android:radius="4dp"/>
</shape>
oval.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义填充渐变颜色 -->
<gradient
android:startColor="#ff0"
android:endColor="#00f"
android:angle="45"
android:type="sweep"/>
<!-- 设置内填充 -->
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp"/>
<!-- 设置圆角矩形 -->
<corners android:radius="8dp"/>
</shape>
以上三个文件建立在drawable-mdpi文件夹下
activity_main.xml
<LinearLayout 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"
android:orientation="vertical">
<EditText android:id="@+id/recetEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/rectangle"/>
<TextView android:id="@+id/rectGradient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gradient_rectangle"/>
<TextView android:id="@+id/oval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/oval"/>
</LinearLayout>
Main.java
package com.example.shapedrawabletest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
本文介绍了Android中Shape Drawable的使用,包括rectangle、gradient_rectangle和oval三种形状的定义,详细讲解了如何设置填充颜色、边框宽度、渐变效果、内边距以及圆角。同时,展示了如何在activity_main.xml布局文件中应用这些Shape Drawable,为EditText和TextView提供背景样式。
1214

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



