如何创建一个EditText圆角而不是默认的矩形角?
EditText
有一种比 CommonsWare 编写的更简单的方法。只需创建一个指定绘制方式的可绘制资源EditText:
<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#FFFFFF" /> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" /> </shape>
然后,只需在您的布局中引用此可绘制对象:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip" android:background="@drawable/rounded_edittext" /> </LinearLayout>
你会得到类似的东西:
根据马克的评论,我想添加为您创建不同状态的方式EditText:
<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext_states.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/rounded_focused" /> <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/rounded_focused" /> <item android:state_enabled="true" android:drawable="@drawable/rounded_edittext" /> </selector>
这些是状态:
<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext_focused.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#FFFFFF"/> <stroke android:width="2dp" android:color="#FF0000" /> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" /> </shape>
而且......现在,EditText应该看起来像:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:background="@drawable/rounded_edittext_states" android:padding="5dip" /> </LinearLayout>