Android 在 TextView 中缩进项目符号列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:30:31  来源:igfitidea点击:

Indent bullet list in TextView

androiduser-interfacetextview

提问by Bj?rn

I have a TextView which I fill with text from a string resources in strings.xml. The string resource contains < li > elements to create a bullet list inside the TextView. My problem is that I want to control the indention of lines in the bullet list that span over more than one line. Default the text isn't indented past the bullet so it looks kind of ugly. Is it possible to do this with style parameters or to create bullets in some other way?

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

Thanks in advance.

提前致谢。

edit: Is it really answered? I don't have any problems producing the bullet list, as described in those links but I'm having problems getting the layout correct. The indentation is like this:

编辑:真的回答了吗?我在生成项目符号列表时没有任何问题,如这些链接中所述,但我在使布局正确时遇到了问题。缩进是这样的:

  • text that go beyond the width
    of the line.
  • 超出行宽
    的文本。

And I want the "of the line" to at least start indented as far as the text after the bullet. That's what I try to achieve.

而且我希望“行”至少从项目符号后面的文本开始缩进。这就是我努力实现的目标。

采纳答案by Bj?rn

I'm suprised that there seems to be noone with this problem. I mean, bullet list can't be that uncommon in about-dialogs, FAQ etc and a bullet doesn't have to contain too much text to span more than one row and run into this problem.

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

Anyway, I got to solve it like this for now:

无论如何,我现在必须像这样解决它:

<?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>

I use it to present static text in a bullet list so I don't bother to create the bullet + text dynamically in code. If anyone have any suggestion how to accomplish the same thing in a better way, please enlight me.

我使用它在项目符号列表中显示静态文本,因此我不必费心在代码中动态创建项目符号 + 文本。如果有人对如何以更好的方式完成同样的事情有任何建议,请启发我。

Btw, if going with the solution suggested in second link above:

顺便说一句,如果采用上面第二个链接中建议的解决方案:

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

The second, third etc. row in a bullet that span over more than one row won't get same indention as first line, which is quite ugly.

跨越多行的项目符号中的第二行、第三行等不会获得与第一行相同的缩进,这非常难看。

回答by Dharmik

Thank you @Bjorn
You can also do something like bellow.

谢谢@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>

回答by Shygar

The way I solved this problem was by using a RelativeLayout and marginLeft. The marginLeft will put a blank margin between it and the previous item.

我解决这个问题的方法是使用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>

回答by Hans Theo Gafriedzal Sulaeman

Just trying to point out the key answer of the question:

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

  • to create an bullet list, use TableLayoutwith two columns for each row. One column for TextViewof a bullet and the other one for the text
  • to make text TextViewfill the rest empty TableRowand indented at new line, set the weight to 1. You can set the bullet weight to zero or just simply not set the bullet weight and let it empty
  • based on my experience, changing width parameter do not affect the text and the bullet. So you can leave it empty or set it to anything you want.
  • 要创建项目符号列表,请使用TableLayout每行两列。一列用于TextView项目符号,另一列用于文本
  • 使文本TextView填充其余空白TableRow并在新行缩进,将权重设置为 1。您可以将子弹重量设置为零,或者只是不设置子弹重量并让它为空
  • 根据我的经验,更改宽度参数不会影响文本和项目符号。因此,您可以将其留空或将其设置为您想要的任何内容。

Example:

例子:

<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>