#appcelerator 文本字段颜色和背景颜色不粘

Posted

技术标签:

【中文标题】#appcelerator 文本字段颜色和背景颜色不粘【英文标题】:#appcelerator textfield color and background color not sticking 【发布时间】:2016-01-17 22:50:40 【问题描述】:

我在我的项目中使用下面的代码作为文本字段:

	var textfield = Ti.UI.createTextField(
		color: 'black',
		height: '40dp',
		top: '5dp',
		left: '5dp',
		right: '50dp',
		style: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
		hintText: 'Enter an address',
		backgroundColor: '#aaa',
		paddingLeft: '5dp'
	);

这适用于我的 ios 模拟器和 iOS 设备,但不适用于 android。我可以在 iOS 上更改颜色(字体颜色)和 backgroundColor,我的 UI 将更改为我想要的十六进制颜色。在android上,无论我做什么,文本和背景总是白色的。在 iOS 上,我可以看到我的提示文本,但在 android 上,它也是白色的。

我觉得我错过了一些简单的东西,但我想不通。

【问题讨论】:

【参考方案1】:

尝试使用 android 的自定义主题。 在platform->android->res->values文件夹中创建两个文件color.xml和custom_theme.xml

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_tab_selected">#E15151</color>
    <color name="text_tab_unselected">#8F8F8F</color>
    <color name="edit_text_hint">#ECECEC</color>
    <color name="blackcolor">#000000</color>
    <color name="whitecolor">#FFFFFF</color>
    <color name="transparent">#00000000</color>
</resources>

custom_theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.NoActionBar" parent="@style/Theme.AppCompat">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowSoftInputMode">adjustPan</item>
    <item name="android:editTextStyle">@style/appEditText</item>
</style>
<style name="Theme.NoActionBar.Translucent" parent="@style/Theme.AppCompat.Translucent">
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowSoftInputMode">adjustPan</item>
    <item name="android:editTextStyle">@style/appEditText</item>
</style>
<style name="appEditText" parent="@android:style/Widget.EditText">
    <item name="android:background">@drawable/edittext_background</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:textColorHint">@color/edit_text_hint</item>
    <item name="android:padding">5dp</item>
</style>
</resources>

现在将此主题应用到 Androidmanifest.xml 中的活动

<application android:theme="@style/Theme.NoActionBar">
    <activity android:name=".YOURACTIVITY" android:label="@string/app_name" android:theme="@style/Theme.NoActionBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

谢谢

【讨论】:

我因病缺阵,但我会尝试一下。我希望有一个更简单的答案。我在 iOS 和 android 上使用 Appcelerator 的其他四个应用程序没有这种样式问题,所以我认为这是移动开发计算机导致的一些明显的配置错误。 是的,你是对的。在我的旧应用程序中,我也没有遇到这个问题。我不记得 SDK 更新的确切版本,但我在更新 SDK 后遇到了这个问题。更新 SDK 后,我遇到了两个问题,一个是标题栏,一个是文本字段提示文本颜色,我使用上面的代码解决了它。 这里的修复应该有效,但没有。我确实让我的原始代码工作了,但只有在删除我的 tiapp.xml 并完全按照我原来的方式手动重新键入它之后。相同的内容。我怀疑如果我删除了所有代码并重新输入了代码,它也会起作用。不得不再次删除并重新输入代码是一种浪费。这不是我第一次不得不简单地重新输入我的代码以使其工作。有谁知道 appcelerator studio 是否随机插入隐藏的控制字符?两次我不得不重新输入所有内容,因为工作室缩小了我的源代码。 不要重新输入代码,只需尝试将 androidmanifest 文件(您将从 build 文件夹中获取)添加到 platform/android 文件夹中,然后将此主题应用到 androidManifest.xml 文件中。

以上是关于#appcelerator 文本字段颜色和背景颜色不粘的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像视图颜色设置为文本字段背景颜色

输入和文本字段中的背景颜色

文本字段背景颜色不变

是否可以为表单中的文本字段提供背景颜色?

如何更改 UISearchController 中文本字段的背景颜色?

如何在 mapbox-gl 中为图层文本字段添加背景颜色