如何创建一个有圆角而不是默认的矩形角的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>
然后,在你的布局中引用这个drawable:
<?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>
你会得到这样的东西:
Edit
根据Mark的评论,我想添加为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>
其他回答
在我看来,它已经有了圆角。
如果你想让它们更全面,你需要:
克隆构成EditText背景的所有9个补丁的PNG图像(在您的SDK中找到) 修改每一个都有更圆角 克隆XML StateListDrawable资源,将这些EditText背景组合成一个Drawable,并将其修改为指向更全面的9个补丁的PNG文件 使用新的StateListDrawable作为EditText小部件的背景
假如你要的只是弯角而不是整尾,则使用下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="10dp" />
<padding
android:bottom="3dp"
android:left="0dp"
android:right="0dp"
android:top="3dp" />
<gradient
android:angle="90"
android:endColor="@color/White"
android:startColor="@color/White" />
<stroke
android:width="1dp"
android:color="@color/Gray" />
</shape>
它只会弯曲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>
然后,在你的布局中引用这个drawable:
<?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>
你会得到这样的东西:
Edit
根据Mark的评论,我想添加为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>
在材质组件库中,你可以使用MaterialShapeDrawable来绘制自定义形状。
使用EditText你可以做:
<EditText
android:id="@+id/edittext"
../>
然后创建一个MaterialShapeDrawable:
float radius = getResources().getDimension(R.dimen.default_corner_radius);
EditText editText = findViewById(R.id.edittext);
//Apply the rounded corners
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable =
new MaterialShapeDrawable(shapeAppearanceModel);
//Apply a background color
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.white));
//Apply a stroke
shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color.colorAccent));
ViewCompat.setBackground(editText,shapeDrawable);
它要求库的版本为1.1.0。
为了补充其他答案,我发现实现圆角的最简单的解决方案是将以下内容设置为Edittext的背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:radius="8dp"/>
</shape>
推荐文章
- Android Studio在哪里保存ProGuard映射文件?
- 如何在Swift中删除视图的所有子视图?
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?
- Android 4.4 (KitKat)上的Android Gallery为Intent返回不同的URI。ACTION_GET_CONTENT
- 移除动作栏下面的阴影
- 如何在线性布局的一侧绘制边界?
- 找到可移动SD卡的位置
- 用于自定义视图的attrs.xml中的同名属性
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本:
- 如何确定Gradle的版本?
- 在android studio中不能小写按钮文本
- 安装在控制台显示错误:安装失败,冲突的提供程序
- 如何在Android中制作倒计时计时器?
- Android中的。dex文件是什么?
- 如何将一些数据传输到另一个片段?