2010-11-15 9 views
5

¿Cómo se ajusta el tamaño de fuente de un Android WebView? El siguiente parece tener ningún efecto:Ajustar el tamaño de fuente de Android WebView

private void fontSizePlus() { 
    fontSize = (fontSize < FONT_SIZE_MAX) ? fontSize + FONT_SIZE_INCREMENT : fontSize; 
    this.changeFontSize(fontSize); 
} 

private void fontSizeMinus() { 
    fontSize = (fontSize > FONT_SIZE_MIN) ? fontSize - FONT_SIZE_INCREMENT : fontSize; 
    this.changeFontSize(fontSize); 
} 

private void changeFontSize(int value) { 
    String js = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '" + value + "%';"; 
    mWebView.loadUrl("javascript:(function() { " + js + " })()"); 
} 

Cuando el WebView y constantes se han inicializado como sigue:

private final static int FONT_SIZE_DEFAULT = 100; 
private final static int FONT_SIZE_MIN = 50; 
private final static int FONT_SIZE_MAX = 150; 
private final static int FONT_SIZE_INCREMENT = 5; 
private int fontSize = FONT_SIZE_DEFAULT; 

private WebView mWebView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.index); 
    mWebView = (WebView) findViewById(R.id.webview); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.loadUrl("file:///android_asset/index.htm"); 
} 

Respuesta

21

La respuesta y el comentario de @rob me indicaron la dirección correcta.

Primero asegúrese de que todos los tamaños de fuente sean relativos al tamaño de fuente predeterminado. Si usa valores absolutos, lo siguiente no funcionará en esos elementos.

continuación:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 
    mWebView = (WebView) findViewById(R.id.webview); 
    fontSize = mWebView.getSettings().getDefaultFontSize(); 
    ... 
} 

private void fontSizePlus() { 
    fontSize++; 
    this.changeFontSize(fontSize); 
} 

private void fontSizeMinus() { 
    fontSize--; 
    this.changeFontSize(fontSize); 
} 

private void changeFontSize(int value) { 
    mWebView.getSettings().setDefaultFontSize(value); 
} 

Cambiando el valor del tamaño de la fuente por defecto todos los tamaños relativos de fuente se ajustan en consecuencia. Esto le da un control mucho mayor que WebSettings.setTextSize (WebSettings.TextSize t).

+0

gracias, funciona;) – anticafe

+0

aquí mi webview es una clase de adaptador cómo usar 'fontSizePlus' y' fontSizeMinus' gracias – NagarjunaReddy

+0

Muchas gracias por su solución :) es realmente útil. – Programmer

4

No sé que webkitTextSizeAdjust se apoya en que no sea Safari Safari/móvil nada .

Trate WebSettings.setTextSize (WebSettings.TextSize t)

en lugar de intentar hacerlo con javascript.

+0

Ese parece ser el problema, sí. Desafortunadamente, WebSettings.TextSize tiene solo 5 valores posibles, que considero demasiado limitantes. ¿Es esto lo mejor que ofrece WebView para este problema? – hpique

+1

Bueno, siempre hay un viejo css. Es decir, ajuste fontSize en el elemento del cuerpo o similar. – rob

+0

+1 Su comentario me indicó la dirección correcta. Publicaremos la solución en breve. – hpique

0

Brian Cooley me ayudó con esto ... probarlo ...

increaseFontButton.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     WebSettings settings = myWebView.getSettings(); 
     settings.setTextZoom((int)(settings.getTextZoom() * 1.1)); 
    } 
} 
Cuestiones relacionadas