Android con párrafo y numeración / viñetas en la vista de texto
Estoy creando una aplicación, que tienen una descripción que tiene pasos. Por ejemplo:
Cómo transferir:
-
-Tienes tarjeta de credito
-
-Si no haces una falsa
-
-A continuación, insertarlo en ATM
-
-Y verlo explotar
Cómo coger un pez:
- Abrir la consola
- Usar trampa
- Tienes una
Y cada vez más …
Esta descripción (pasos) se mostrará en una vista de desplazamiento, ahora mismo lo hago usando java, setText usa "\ n" y "" espacio vacío adicional para engañar el juego. Sin embargo esto no funcionará para diferentes resoluciones. ¿Cómo hacer que textview muestre algo como esto?
Basado en la respuesta, he intentado esto: .xml en el diseño
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="16dp" android:layout_marginTop="16dp" > <TextView android:id="@+id/tvCBAjud" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/transfer" android:textSize="16sp" /> </ScrollView>
Y definir la cadena:
<string name="transfer">how to transfer \n <ul> <li>-Do you have credit card</li> <li>-If you dont make a fake one</li> <li>-Then insert it to ATM</li> <li>-And watch it explode</li> </ul></string>
Pero la salida se convierte en:
How to transfer -Do you have credit card -If you dont make a fake one -Then insert it to ATM -And watch it explode
Si su texto es estático, puede extender TextView
para TextView
automáticamente un BulletSpan
antes de su android:text
:
Layout.xml
<BulletTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Orange" /> <BulletTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Banana" />
BulletTextView.java
/** * {@link TextView} that automatically adds bullet point to text if set in layout */ public class BulletTextView extends TextView { public BulletTextView(Context context) { super(context); addBullet(); } public BulletTextView(Context context, AttributeSet attrs) { super(context, attrs); addBullet(); } public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addBullet(); } public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); addBullet(); } private void addBullet() { CharSequence text = getText(); if (TextUtils.isEmpty(text)) { return; } SpannableString spannable = new SpannableString(text); spannable.setSpan(new BulletSpan(16), 0, text.length(), 0); setText(spannable); } }
Puede personalizarlo adicionando un atributo personalizado para la sangría.
Puede utilizar Unicode como viñetas
TextView tv = (TextView) findViewById(R.id.textView1); String circle = "\u25CF"; String sentence = circle + " " + "Hello its me"+ "\n"; tv.setText(sentence);
Utilice cualquier símbolo, viñetas, etc cambiando Unicode y Unicode Tabla está aquí.
http://unicode-table.com/en/#box-drawing
En el string.xml escriba el código siguiente:
<string name="transfer">how to transfer \n<ul><li>-Do you have credit card</li> <li>-If you dont make a fake one</li> <li>-Then insert it to ATM</li> <li>-And watch it explode</li> </ul></string> <string name="catch"><ul><li>Open console</li> <li>use cheat</li> <li>you got one</li> </ul></string>
Y en su activity.xml
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/transfer" android:textSize="@dimen/textsize_16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/catch" android:textSize="@dimen/textsize_16sp" />
Puede utilizar <br>
lugar de \ ny llamar a HTML.fromHtml
para convertirlo en HTML.
Me gusta esto:
String sHTML = " <string name=\"transfer\">how to transfer<br>" + "<br>" + "• Do you have credit card<br>" + "• If you dont make a fake one<br>" + "• Then insert it to ATM<br>" + "• And watch it explode<br>" + "</string>"; TextView textView1 = (TextView)findViewById(R.id.tvCBAjud); textView1.setText(Html.fromHtml(sHTML, null, null));
Usted puede personalizar BulletSpan en su propia manera como esto:
public class CustomBulletSpan implements LeadingMarginSpan, ParcelableSpan { private final int mGapWidth; private final boolean mWantColor; private final int mColor; private static final int BULLET_RADIUS = 3; private static Path sBulletPath = null; public static final int STANDARD_GAP_WIDTH = 2; public CustomBulletSpan(int gapWidth) { mGapWidth = gapWidth; mWantColor = false; mColor = 0; } public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mGapWidth); dest.writeInt(mWantColor ? 1 : 0); dest.writeInt(mColor); } public int getLeadingMargin(boolean first) { return 2 * BULLET_RADIUS + mGapWidth; } public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout l) { // Here I shifted the bullets to right by the given half bullet x += mGapWidth / 2; if (((Spanned) text).getSpanStart(this) == start) { Paint.Style style = p.getStyle(); int oldcolor = 0; if (mWantColor) { oldcolor = p.getColor(); p.setColor(mColor); } p.setStyle(Paint.Style.FILL); if (c.isHardwareAccelerated()) { if (sBulletPath == null) { sBulletPath = new Path(); // Bullet is slightly better to avoid aliasing artifacts on // mdpi devices. sBulletPath.addCircle(0.0f, 0.0f, 1.2f * BULLET_RADIUS, Direction.CW); } c.save(); c.translate(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f); c.drawPath(sBulletPath, p); c.restore(); } else { c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p); } if (mWantColor) { p.setColor(oldcolor); } p.setStyle(style); } } @Override public int getSpanTypeId() { return 0; } }