如果我使用下面的代码更改了我的 EditText
的背景颜色,看起来该框缩小了,并且它不会保持默认 EditText
存在的蓝色底部边框的ICS主题 .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99000000"
>
<EditText
android:id="@+id/id_nick_name"
android:layout_marginTop="80dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:orientation="horizontal"
android:layout_below="@+id/id_nick_name">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="add"
android:layout_weight="1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cancel"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
这是它的样子:
9 回答
您应该做的是为edittext创建9补丁图像并将该图像设置为编辑文本背景 . 您可以使用this网站创建9个补丁
我附上了一个样本9补丁图像供您参考 . 使用它作为编辑文本背景,你会得到一个想法 . 右键单击图像并选择“将图像另存为” . 保存图像时别忘了将其扩展名设为“9.png”
一行惰性代码:
这是最好的方法
首先:在
res
/drawable
中创建新的xml
文件,将其命名为rounded_edit_text
然后粘贴此:第二:在res / layout复制和过去的代码中(代码为
EditText
)我创建了color.xml文件,用于命名我的颜色名称(黑色,白色......)
在EditText中,设置颜色
或者在style.xml中使用样式:
并将ctreated样式添加到EditText:
你正在使用的颜色是白色"#ffffff"是白色的,所以如果你想要的话,尝试在值中进行不同的改变,直到你从这个链接得到你的需要Color Codes它应该没问题
我发现最简单的解决方案是以编程方式更改背景颜色 . 这不需要处理任何9补丁图像:
资料来源:another answer
您应该使用样式而不是背景颜色 . 尝试搜索holoeverywhere然后我认为这将帮助您解决您的问题
Using holoeverywhere
只需更改一些9patch资源即可自定义edittext外观 .
对我来说这个代码是有用的所以把这个代码放在XML文件rounded_edit_text中
经过2天的努力,我找到了解决这个问题的工作方案,下面的解决方案非常适合那些只想更改少量编辑文本,通过java代码更改/切换颜色,并希望克服OS版本上不同行为问题的人由于使用了setColorFilter()方法 .
在xml中使用
添加样式
用于切换颜色的java代码