我在Android WebView
的HTML頁面中獲得了輸入標籤。我希望背景透明。它是(通過background-color: transparent
)直到用戶選擇它(給它焦點,輸入數據)。然後背景是白色的。如何設置所選文本框的Android WebView背景
我試過background-color
對textbox
和textbox:focus
而我試過-webkit-appearance: none
。這些都沒有奏效。
有人與此混淆?
我在Android WebView
的HTML頁面中獲得了輸入標籤。我希望背景透明。它是(通過background-color: transparent
)直到用戶選擇它(給它焦點,輸入數據)。然後背景是白色的。如何設置所選文本框的Android WebView背景
我試過background-color
對textbox
和textbox:focus
而我試過-webkit-appearance: none
。這些都沒有奏效。
有人與此混淆?
我可能是錯的,但這實際上看起來像是android.webkit包代碼中的一個bug。 WebView
似乎使用android.webkit.TextDialog
類來表示即使在呈現的HTML中的輸入框。
但是,該代碼隱藏了webkit的渲染與android的文本小部件。注意LayerDrawable變量圖層,調用setBackgroundDrawable()和LayerDrawable中的兩個圖層,其中最下面的圖層設置爲總是爲白色。
正如你所看到的,WebCore呈現的文本在這裏隱藏在這個白色背景之後。評論甚至進一步明確說明了這一點。
當然,WebView.java中的TextDialog變量mTextEntry在文本框聚焦之前不會被聚焦,所以直到不隱藏什麼webkit呈現。
/**
* Create a new TextDialog.
* @param context The Context for this TextDialog.
* @param webView The WebView that created this.
*/
/* package */ TextDialog(Context context, WebView webView) {
super(context);
mWebView = webView;
ShapeDrawable background = new ShapeDrawable(new RectShape());
Paint shapePaint = background.getPaint();
shapePaint.setStyle(Paint.Style.STROKE);
ColorDrawable color = new ColorDrawable(Color.WHITE);
Drawable[] array = new Drawable[2];
array[0] = color;
array[1] = background;
LayerDrawable layers = new LayerDrawable(array);
// Hide WebCore's text behind this and allow the WebView
// to draw its own focusring.
setBackgroundDrawable(layers);
// Align the text better with the text behind it, so moving
// off of the textfield will not appear to move the text.
setPadding(3, 2, 0, 0);
mMaxLength = -1;
// Turn on subpixel text, and turn off kerning, so it better matches
// the text in webkit.
TextPaint paint = getPaint();
int flags = paint.getFlags() | Paint.SUBPIXEL_TEXT_FLAG |
Paint.ANTI_ALIAS_FLAG & ~Paint.DEV_KERN_TEXT_FLAG;
paint.setFlags(flags);
// Set the text color to black, regardless of the theme. This ensures
// that other applications that use embedded WebViews will properly
// display the text in textfields.
setTextColor(Color.BLACK);
setImeOptions(EditorInfo.IME_ACTION_NONE);
}
在git中的source code。
看來這已在Android 2.1中修復。 – 2010-02-23 00:19:40