tamaño - listview personalizado android studio ejemplo
Cómo establecer el tamaño del texto en WebView en Android (7)
1 - Estoy usando este código que me da un buen resultado en todos mis dispositivos.
Si la web es poco WebView,
web = (WebView) v.findViewById(R.id.htmlDisplay);
// Impostazioni della WebView.
final WebSettings webSettings = web.getSettings();
// Set the font size (in sp).
webSettings.setDefaultFontSize(20);
OK, esto está codificado
Ahora, una solución más dinámica (según su pregunta):
Si coloca el tamaño deseado en un archivo de enteros (varios, uno para cada pantalla que admite), puede hacerlo para recuperar su valor:
Resources res = getResources();
float fontSize = res.getInteger(R.integer.font_size);
webSettings.setDefaultFontSize(fontSize);
Suponiendo que tus archivos res / values / integers.xml son similares a este:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="font_size">20</integer>
</resources>
Así que cambie esta línea en el código anterior
webSettings.setDefaultFontSize(20);
a
webSettings.setDefaultFontSize(fontSize);
2 - Para cargar cosas más rápido, uso el siguiente código
// Optimize loading times.
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);
Tenga en cuenta que cargo más de 7-8 líneas de texto: es una página HTML completa con JavaScript para expandir / contraer secciones. Y lleva, digamos, medio segundo cargar. Entonces, supongo que se cargarán de 7 a 8 líneas de texto en un instante.
Estoy usando WebView para justificar el texto. Quiero saber-
¿Es posible establecer el tamaño del texto en el archivo de
layout? Como quiero un tamaño de texto diferente para diferentes tamaños de pantalla.Y también un problema es que primero aparece el fondo luego de 2 segundos de visualización de texto. ¿Es posible mostrar texto de inmediato?
El tamaño del texto es 7-8 líneas .
Código-
public class Main extends Activity {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
String text = "<html><body>"+"<p align=/"justify/">"+getString(R.string.itext)+"</p>"+"</body></html>";
mWebView .loadData(text, "text/html", "utf-8");
mWebView .setBackgroundColor(Color.TRANSPARENT);
}
}
Xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/light">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"/>
</LinearLayout>
Hay tres formas de establecer FontSize en WebView .
1) WebView.setDefaultFontSize (int)
webview.setDefaultFontSize(50);
2) WebView.getSettings (). SetTextSize (WebSettings.TextSize.SMALLEST)
WebView.getSettings().setTextSize(TextSize.SMALLEST);
o
WebView.getSettings().setTextSize(TextSize.SMALLER);
o
WebView.getSettings().setTextSize(TextSize.NORMAL);
o
WebView.getSettings().setTextSize(TextSize.BIGGER);
o
WebView.getSettings().setTextSize(TextSize.BIGGEST);
3) Agregue tamaño de fuente en formato html.
String text = "<html><body style=/"color:black;font-family:Helvetica;font-size:12px;/"''background-color:transparent'' >"+"<p align=/"justify/">"+getString(R.string.itext)+"</p>"+"</body></html>";
No estoy seguro de si esto es lo que está buscando, pero siempre que no desee establecer el tamaño del texto en base a un cálculo con el ancho / alto del texto, esto debería ser lo que está buscando:
WebSettings settings = yourWebView.getSettings();
De esta forma obtendrá la configuración de WebView cuyo tamaño de texto está tratando de cambiar.
Hay algunos valores predefinidos que puede establecer como tamaño utilizando la clase TextSize , por ejemplo:
settings.setTextSize(TextSize.SMALLEST);
settings.setTextSize(TextSize.SMALLER);
settings.setTextSize(TextSize.NORMAL);
settings.setTextSize(TextSize.BIGGER);
settings.setTextSize(TextSize.BIGGEST);
Simplemente puede elegir cuál necesita configurar dependiendo de los parámetros del dispositivo actual que elija.
Si, por el contrario, quiere hacer algún tipo de cálculo del ancho / alto de la pantalla y establecer el resultado como un valor particular, incluso puede usar esto:
settings.setDefaultFontSize(an_integer_that_goes_between_1_and_72);
---- EDIT ----
Para realzar su renderizado WebView , intente lo siguiente:
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
También puede intentar agregar esto a su archivo Manifest :
android:hardwareAccelerated="true"
Para configurar el tamaño del texto desde el diseño-
final WebSettings webSettings = web.getSettings();
Resources res = getResources();
fontSize = res.getDimension(R.dimen.txtSize);
webSettings.setDefaultFontSize((int)fontSize);
Para visualización de texto inmediata-
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);
En la carpeta de valores-
<resources>
<dimen name="txtSize">26sp</dimen>
</resources>
Espero que funcione.
Puede usar Html.fromHtml() y escribir su texto en formato HTML .
Siempre puedes usar CSS para cambiar el tamaño de la fuente:
String text = "<html><style type=/"text/css/">p{text-align:justify;font-size:80%;}</style></head><body>"+"<p>"+getString(R.string.text1)+"</p>"+"</body></html>";
mWebView.loadData(text, "text/html", "utf-8");
Para mostrar Fater, como dijo Digvesh Patel antes:
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
WebSettings webSettings = webView.getSettings();
ya sea setTextSize o
webSettings.setTextSize(WebSettings.TextSize.SMALLEST);
Este también funciona:
webSettings.setDefaultFontSize(10);
Y Mostrar texto de inmediato:
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setBlockNetworkImage(true);
webview.setLoadsImagesAutomatically(true);
webwebviewSettings.setGeolocationEnabled(false);
webview.setNeedInitialFocus(false);
webview.setSaveFormData(false);