2010-09-15 46 views
24

我有一个TextView,它使用strings.xml中的字符串资源填充文本。字符串资源包含< li>元素以在TextView内创建项目符号列表。我的问题是我想控制项目符号列表中多行的缩进。默认情况下,文本不会缩进子弹,所以它看起来很丑。是否有可能通过样式参数来做到这一点,或以其他方式创建子弹?在TextView中缩进项目符号列表

在此先感谢。

编辑: 真的回答了吗?我没有任何问题产生子弹列表,如这些链接中所述,但我有问题得到布局正确。缩进是这样的:

  • 文字超出宽度
    的行。

我希望“行”至少开始缩进,直到项目符号后的文本。这就是我试图达到的目标。

+0

此问题已被要求。可能的重复http://stackoverflow.com/questions/3429546/android-how-to-add-bullet-symbol-in-textview http://stackoverflow.com/questions/2196499/ordered-lists-inside-an- android-textview – DeRagan 2010-09-15 10:07:19

+1

@DeRagan,我不认为这是重复的。你提到的问题是要求有一个子弹符号,而不是一个项目符号列表。 – 2011-10-08 12:31:53

回答

20

我很惊讶,似乎没有这个问题。我的意思是,项目符号列表在关于对话框,常见问题解答等方面并不常见,并且子弹不必包含太多文本以跨越多行并遇到此问题。

不管怎样,我得解决它像这样现在:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    android:id="@+id/ScrollViewTipsLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/TipsLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TableLayout 
     android:layout_height="wrap_content" 
     android:id="@+id/TableLayout01" 
     android:layout_width="wrap_content" 
    > 
     <TableRow> 
      <TextView android:id="@+id/tvIngress" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="@+string/tv_picking_content_ingress" 
       android:layout_span="2" 
       android:singleLine="false" 
       android:layout_weight="1" 
      /> 
     </TableRow> 
     <TableRow> 
      <TextView android:id="@+id/tvCleaningDot1" 
       android:layout_height="wrap_content" 
       android:text="•" 
       android:singleLine="false" 
      /> 
      <TextView android:id="@+id/tvCleaningFirst" 
       android:layout_height="wrap_content" 
       android:text="@+string/tv_picking_content_first" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:gravity="left" 
       android:singleLine="false" 
      /> 
     </TableRow> 
      <TextView android:id="@+id/tvCleaningDot2" 
       android:layout_height="wrap_content" 
       android:text="•" 
       android:singleLine="false" 
      /> 
      <TextView android:id="@+id/tvCleaningSecond" 
       android:layout_height="wrap_content" 
       android:text="@+string/tv_picking_content_second" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:gravity="left" 
       android:singleLine="false" 
      /> 
     </TableRow> 
    </TableLayout> 
</RelativeLayout> 

我用它来展示项目符号列表中的静态文本,所以我也懒得去创建项目符号+文本动态地在代码中。如果任何人有任何建议如何以更好的方式完成同样的事情,请让我高兴。

顺便说一句,如果与解决方案会建议在第二个以上链接:

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

在子弹的第二,第三等一行跨越多行不会得到相同的缩进作为一线,这很丑陋。

+0

谢谢,而且如果你想为bullete添加自定义图片,我们可以这样做 – Dharmik 2013-05-17 11:36:59

0

我解决这个问题的方法是使用RelativeLayout和marginLeft。 marginLeft将在它和前一个项目之间留出一个空白边距。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track" 
     android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb" 
     android:scrollbarSize="12dip"> 

<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10sp"> 

    <TextView 
     android:id="@+id/body_text3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:lineSpacingExtra="3sp" 
     android:text="Main paragraph of text, before the bulleted list" 
     android:textSize="15sp" /> 
    <TextView 
     android:id="@+id/type1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:lineSpacingExtra="3sp" 
     android:text="• First bullet" 
     android:textSize="15sp" 
     android:layout_below="@+id/body_text3" 
     android:layout_marginLeft="25dp" /> 
    <TextView 
     android:id="@+id/type2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:lineSpacingExtra="3sp" 
     android:text="• Second bullet" 
     android:textSize="15sp" 
     android:layout_below="@+id/type1" 
     android:layout_marginLeft="25dp" /> 
</RelativeLayout> 

</ScrollView> 
+0

与[Björn的回答](http://stackoverflow.com/a/3871309/94363)有什么不同? – rds 2014-07-21 09:16:53

+1

我建议使用文本视图的'android:lineSpacingExtra =“3sp”',而不是使用。不是说它更好,而是更好的选择。 – Shygar 2014-07-22 20:16:26

0

只是想指出问题的关键答案:

  • 创建一个符号列表,使用TableLayout有两列的每一行。一列为子弹TextView,另一列为文字
  • 使文字TextView填充其余为空TableRow并在新行缩进,将权重设置为1.您可以将子弹重量设置为零或者根本不设置设置子弹重量并让它为空
  • 根据我的经验,改变宽度参数不会影响文本和子弹。所以你可以把它留空或者把它设置成任何你想要的。

例子:

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/bullet" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/bullet"/> 
     <TextView 
      android:id="@+id/TextView01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="this is the text of a bullet list"/> 
    </TableRow> 
</TableLayout> 
4

谢谢@Bjorn
你也可以这样做波纹管。

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/point" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     /> 

     <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"     
     android:text="Your Text Here" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="14sp" /> 
</LinearLayout> 
+0

这是完美的。如果我想循环使用多个文本视图,该怎么办?我怎样才能做到这一点? – Raphael 2017-12-21 10:10:51