Indent bullet list in TextView

2020-06-07 02:07发布

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?

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.

4条回答
一夜七次
2楼-- · 2020-06-07 02:26

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.

<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>
查看更多
家丑人穷心不美
3楼-- · 2020-06-07 02:28

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

  • to create an bullet list, use TableLayout with two columns for each row. One column for TextView of a bullet and the other one for the text
  • to make text TextView fill the rest empty TableRow and 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.

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>
查看更多
疯言疯语
4楼-- · 2020-06-07 02:30

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.

查看更多
在下西门庆
5楼-- · 2020-06-07 02:42

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

<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>
查看更多
登录 后发表回答