android edittext去除横线
时间: 2023-07-22 18:43:35 浏览: 152
在 Android 中,可以使用以下两种方式去除 EditText 的下划线:
1. 在布局文件中设置 `background` 属性
可以在 EditText 的布局文件中设置 `background` 属性,并将其设置为 `null`,这样就可以去除 EditText 的下划线。示例代码如下:
```xml
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null" />
```
在这个示例中,我们在 EditText 中设置了 `android:background="@null"` 属性,这样就可以去除 EditText 的下划线。
2. 在样式文件中设置 `editTextStyle`
可以在样式文件中定义一个新的 `editTextStyle`,并在其中设置 `android:background` 属性为 `null`,然后将该样式应用到 EditText 上。示例代码如下:
在 `styles.xml` 文件中定义样式:
```xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 定义一个新的 EditText 样式 -->
<item name="editTextStyle">@style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<!-- 将 background 属性设置为 null -->
<item name="android:background">@null</item>
</style>
```
在布局文件中使用 EditText,并将 AppTheme 主题应用到该布局中:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppTheme">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
在这个示例中,我们在样式文件中定义了一个新的 EditText 样式,并将其中的 `android:background` 属性设置为 `null`,然后将该样式应用到 EditText 上。注意,在布局文件中我们还将 AppTheme 主题应用到了 LinearLayout 中,这样才能使 EditText 应用我们定义的样式。
阅读全文
相关推荐















