android 带下边框线的输入框
时间: 2023-10-30 14:03:48 浏览: 892
Android中带有下边框线的输入框可以通过在XML布局文件或者Java代码中进行设置,以下给出两种常见的实现方法。
第一种方法是通过在XML布局文件中使用shape drawable进行设置。首先,创建一个`bg_edittext.xml`的文件,在该文件中定义一个shape drawable来描绘输入框的背景样式:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" /> <!-- 输入框的背景颜色 -->
<stroke
android:width="2dp" <!-- 下边框的宽度 -->
android:color="#000000" /> <!-- 下边框的颜色 -->
<padding
android:top="10dp" <!-- 输入框的内边距 -->
android:bottom="10dp" />
</shape>
```
接下来,在XML布局文件中使用自定义的背景样式来设置输入框:
```xml
<EditText
android:id="@+id/my_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext" />
```
第二种方法是通过在Java代码中动态地设置输入框的背景样式。在Activity或者Fragment的`onCreate()`方法中,首先创建一个`ShapeDrawable`对象来定义输入框的背景样式:
```java
ShapeDrawable shape = new ShapeDrawable();
shape.getPaint().setColor(Color.WHITE); // 输入框的背景颜色
shape.getPaint().setStrokeWidth(2); // 下边框的宽度
shape.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
shape.getPaint().setStrokeJoin(Paint.Join.ROUND);
shape.getPaint().setStrokeCap(Paint.Cap.ROUND);
shape.setPadding(0, 10, 0, 10); // 输入框的内边距
shape.getPaint().setColor(Color.BLACK); // 下边框的颜色
```
然后,获取到要设置下边框线的输入框的实例,并应用上面创建的背景样式:
```java
EditText editText = findViewById(R.id.my_edittext);
editText.setBackground(shape);
```
以上就是两种常见的实现Android带下边框线的输入框的方法。根据需求和使用场景,选择其中一种进行使用即可。