EditText后台(android)存在问题
我有一个编辑文本背景这样的问题EditText后台(android)存在问题,android,Android,我有一个编辑文本背景这样的问题 <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="35sp" android:singleLine="true" android:lay
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:singleLine="true"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:layout_marginTop="5px"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
在尝试设置背景后,情况看起来更糟
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:singleLine="true"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:layout_marginTop="5px"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#ffffff"
/>
EditText背景会发生什么?如何使EditText保持默认样式?因为我认为您应该更改背景颜色,而不是背景。因为它使用xml自定义形状
- 可以用作背景的绘图工具。这可以是对完整可绘制资源的引用(例如PNG图像、9-patch、XML状态列表描述等),也可以是纯色,例如
(黑色)#ff000000
- 可以是对另一个资源的引用,格式为
,或对主题属性的引用,格式为@[+][package:]type:name
e?[package:[type:]nam
- 可以是颜色值,其形式为
、#rgb
、#argb
或#rrggbb
#aarggbb
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edittext_selector">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="@drawable/your_ninepath_image">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="@drawable/your_ninepath_image">
</item>
<!-- Default state -->
<item android:state_enabled="true"
android:drawable="@drawable/your_ninepath_image">
</item>
</selector>
❷在EditText xml集合选择器上:
<EditText
...
android:background="@layout/**edittext_selector**"
...
</EditText>
如果您希望在不完全更改背景图像的情况下动态编辑Android背景的颜色,请尝试以下方法:(这可能不是最好的解决方案,但很有效):
我的解决方案是一行代码:
<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).
.getBackground().setColorFilter(Color.,PorterDuff.Mode.MULTIPLY);)。
它分解如下:
- “getBackground()”从组件获取背景
- “setColorFilter”将调用对背景图像本身的过滤
- “颜色”。确定要传递到过滤器上的颜色
- “PorterDuff.Mode.”设置要对给定颜色和获取的背景图像进行的操作类型
熟悉图像编辑软件的人可能会识别该模式。每种模式对背景图像的颜色应用方式都有一定的影响。要简单地“覆盖”图像的颜色,在保留其梯度、边框等的同时,使用乘法
您不需要创建图像。android系统中有一个内置图像,您可以使用。因此,请按以下方式编辑xml格式的EditText-
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:hint="@string/campaign_message"
android:inputType="textMultiLine"
android:background="@android:drawable/editbox_background_normal"
android:minHeight="80dp" >
</EditText>
注意这行:android:background=“@android:drawable/editbox\u background\u normal”
我必须使用SRC\u-top才能让它为我工作
mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
是不是android:textAppearance=“@android:attr/textAppearanceSmall”事实上-文本的语法是正确的,请参见下面我的答案。他用于背景色的语法是validokay关于语法,我认为他用阴影等覆盖默认的背景xml形状,并将其设置为纯色。你没有抓住要点吗?当你设置编辑文本的背景色时,内置的可绘制图形将被覆盖。除非如果你已经将你的EditText主题从默认的drawable中移除,那么就没有理由使用“android:backgroun=“@android:drawable/editbox\u background\u normal”这正是我想要的。谢谢!
mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);