2010-02-04 18 views
9

Android que quieren mostrar una lista ordenada dentro de un TextView, por ejemplo:
1) Artículo 1
2) Artículo 2Las listas ordenadas dentro de un TextView

Usando la siguiente distribución:

<TextView 
    android:text="<ol><li>item 1\n</li><li>item 2\n</li></ol> 
    /> 

me sale:

  • artículo 1
  • artículo 2

¿Cómo puedo cambiar las balas a los números?

Gracias.

Respuesta

1

Se puede utilizar esta manera siguiente:

&#8226; foo<br/> 
&#8226; bar<br/> 
&#8226; baz<br/> 
+0

balas Obtención lugar de números :( –

9

yo creo que hay que hacer esto en el código. Tuve que crear una subclase LeadingMarginSpan para que esto funcione. Así es como lo hice.

private class NumberIndentSpan implements LeadingMarginSpan { 

    private final int gapWidth; 
    private final int leadWidth; 
    private final int index; 

    public NumberIndentSpan(int leadGap, int gapWidth, int index) { 
     this.leadWidth = leadGap; 
     this.gapWidth = gapWidth; 
     this.index = index; 
    } 

    public int getLeadingMargin(boolean first) { 
     return leadWidth + gapWidth; 
    } 

    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) { 
     if (first) { 
      Paint.Style orgStyle = p.getStyle(); 
      p.setStyle(Paint.Style.FILL); 
      float width = p.measureText("4."); 
      c.drawText(index + ".", (leadWidth + x - width/2) * dir, bottom - p.descent(), p); 
      p.setStyle(orgStyle); 
     } 
    } 
} 

hacerse con su punto de vista, y lo utilizan como esto:

SpannableStringBuilder ssb = new SpannableStringBuilder(); 
for(String text : list) { 
    int contentStart = content.length(); 
    content.append(text); 
    content.setSpan(new NumberIndentSpan(15, 15, number), contentStart, content.length(), 0); 
} 

TextView view = findViewById(R.id.....); 
view.setText(ssb); 

Espero que esto ayude a otros en busca de esta :-)

+1

Hey puedes decirme lo que es ** "lista" ** y ** "texto" ** representa. es decir dónde exactamente ha definido esto y cómo hace esto funciona. realmente aprecio si se pasa todo el código aquí. – InnocentKiller

+0

** "lista" ** es la lista que contiene las líneas que desea para formatear. Y ** "texto" ** debería haberse llamado ** l ** (La línea que desea t para formatear) –

1

Aquí está una solución que uso. Puede copiar y pegar en una actividad para ver cómo funciona, pero debe cambiar todos los atributos con variables de producción. Puede jugar con los parámetros de relleno para aplicar sangría de acuerdo a sus necesidades. En lugar de dígitos, puede usar la viñeta si desea la lista de viñetas.

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/bullet1" 
       android:textStyle="bold" 
       android:layout_width="30dp" 
       android:gravity="right" 
       android:layout_height="wrap_content" 
       android:paddingRight="5dp" 
       android:text="1" 
       android:textSize="20dp" /> 

      <TextView 
       android:id="@+id/bullet1Text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="10dp" 
       android:text="First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. " 
       android:textSize="15dp" /> 
     </LinearLayout> 



     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/bullet2" 
       android:textStyle="bold" 
       android:layout_width="30dp" 
       android:gravity="right" 
       android:layout_height="wrap_content" 
       android:paddingRight="5dp" 
       android:text="2" 
       android:textSize="20dp" /> 

      <TextView 
       android:id="@+id/bullet2Text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="10dp" 
       android:text="Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. " 
       android:textSize="15dp" /> 
     </LinearLayout> 
0

Podemos utilizar directamente LeadingMarginSpan

por ejemplo

String[] textArray = { 
    "dfsdljjlfsdsdfjsdjldssdfidfsjdljasdfjfds\n", 
    "sdfjdfjlkfdjdfkfjiwejojodljfldsjodsjfsdjdlf\n", 
    "djsdfjsdffjdflljfjsadfdjfldfjl" 
}; 

SpannableStringBuilder content = new SpannableStringBuilder(); 
int number = 1; 
for (String t1 : textArray) { 
    int contentStart = content.length(); 

    String leadingString = number + ". "; 
    content.append(leadingString); 
    content.append(t1); 

    int contentEnd = content.length(); 
    content.setSpan(
      new LeadingMarginSpan.Standard(0, 66), 
      contentStart, 
      contentEnd, 
      Spannable.SPAN_INCLUSIVE_EXCLUSIVE 
    ); 

    number++; 
} 
Cuestiones relacionadas