Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将项目符号列表添加到android应用程序?_Android_Textview - Fatal编程技术网

如何将项目符号列表添加到android应用程序?

如何将项目符号列表添加到android应用程序?,android,textview,Android,Textview,我已经用谷歌搜索了我的问题,但没有提供有效的答案。如何将项目符号列表添加到我的文本视图。创建项目符号列表的两个选项是 使用html(ul,ol)创建列表,并将html加载到Web视图中 将数据加载到列表视图中,并将列表项布局中文本视图的左侧可绘制部分设置为适合项目符号的图像 选项1是最简单的。很难做到,因为不支持ul/li/ol。幸运的是,您可以将其用作语法糖: &#8226; foo<br/> &#8226; bar<br/> &#8226;

我已经用谷歌搜索了我的问题,但没有提供有效的答案。如何将项目符号列表添加到我的文本视图。

创建项目符号列表的两个选项是

  • 使用html(ul,ol)创建列表,并将html加载到Web视图中
  • 将数据加载到列表视图中,并将列表项布局中文本视图的左侧可绘制部分设置为适合项目符号的图像

选项1是最简单的。

很难做到,因为不支持ul/li/ol。幸运的是,您可以将其用作语法糖:

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

我使用的一个选项是使用样式将项目符号设置为可绘制

<style name="Text.Bullet">
    <item name="android:background">@drawable/bullet</item>
    <item name="android:paddingLeft">10dp</item>
</style>

@可牵引/子弹
10dp
用法:

<TextView android:id="@+id/tx_hdr" 
android:text="Item 1" style="@style/Text.Bullet" />

  • 提供的带有html实体的解决方案可能很有用。但它只包括子弹。如果文本换行,缩进将不正确

  • 我发现了其他嵌入web视图的解决方案。这也许对某些人来说是合适的,但我认为这有点过分了。。。(与使用列表视图相同。)

  • 我喜欢:D,但它不允许向文本视图添加无序列表

  • 我的带有项目符号的无序列表示例

  • 肯定的:

    • 文本换行后具有正确缩进的项目符号
    • 您可以在TextView的一个实例中组合其他格式化或未格式化的文本
    • 您可以在BulletSpan构造函数中定义缩进的大小
    否定:

    • 您必须将列表中的每一项保存在单独的字符串资源中。所以你不能像在HTML中那样轻松地定义你的列表

      • 我找到了一个替代品。。只需复制此项目符号“•”(它是一个文本)并粘贴到文本视图的文本中,就可以通过更改textcolor以及所有其他属性(如大小、高度和宽度…)来更改项目符号的颜色

        你可以用捷径在打字时得到这个项目符号

        窗户

        ALT+7

        对于mac

        ALT+8


        这是一个项目符号列表,每个项目前面都有一个标题和一个选项卡

        public class BulletListBuilder {
        
            private static final String SPACE = " ";
            private static final String BULLET_SYMBOL = "&#8226";
            private static final String EOL = System.getProperty("line.separator");
            private static final String TAB = "\t";
        
            private BulletListBuilder() {
        
            }
        
            public static String getBulletList(String header, String []items) {
                StringBuilder listBuilder = new StringBuilder();
                if (header != null && !header.isEmpty()) {
                    listBuilder.append(header + EOL + EOL);
                }
                if (items != null && items.length != 0) {
                    for (String item : items) {
                        Spanned formattedItem = Html.fromHtml(BULLET_SYMBOL + SPACE + item);
                        listBuilder.append(TAB + formattedItem + EOL);
                    }
                }
                return listBuilder.toString();
            }
        
        }
        

        通过使用字符串资源中的
      • 标记,可以简单地创建项目符号列表

        不要使用setText(Html.fromHtml(string))在代码中设置字符串!只需在xml中或使用setText(string)正常设置字符串

        例如:

        strings.xml文件

        
        
        • 第一项
        • 项目2

        layout.xml文件

        
        

        它将产生以下结果:

        • 第一项
        • 第2项

          • 支持以下标记(直接嵌入到字符串资源中):

            • (支持属性“href”)
            • (支持属性“高度”、“大小”、“fgcolor”和“双色”作为整数)

            我发现这是最简单的方法,将textView保留在xml文件中,并使用以下java代码。这对我来说非常好

            private static final String BULLET_SYMBOL = "&#8226";
            
            @Override
            protected void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_tutorial);
            
                TextView tv = (TextView) findViewById(R.id.yourTextView);
            
                tv.setText("To perform this exercise you will need the following: "
                                    + System.getProperty("line.separator")//this takes you to the next Line
                                    + System.getProperty("line.separator")
                                    + Html.fromHtml(BULLET_SYMBOL + " Bed")
                                    + System.getProperty("line.separator")
                                    + Html.fromHtml(BULLET_SYMBOL + " Pillow"));
            }
            

            完全过火了,创建了自定义文本视图

            像这样使用它:

            <com.blundell.BulletTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="--bullet 1 --bullet two --bullet three --bullet four" />
            
            
            
            以及守则:

            package com.blundell;
            
            import android.content.Context;
            import android.text.Html;
            import android.util.AttributeSet;
            import android.widget.TextView;
            
            public class BulletTextView extends TextView {
                private static final String SPLITTER_CHAR = "--";
                private static final String NEWLINE_CHAR = "<br/>";
                private static final String HTML_BULLETPOINT = "&#8226;";
            
                public BulletTextView(Context context, AttributeSet attrs) {
                    this(context, attrs, android.R.attr.textViewStyle);
                }
            
                public BulletTextView(Context context, AttributeSet attrs, int defStyle) {
                    super(context, attrs, defStyle);
                    checkForBulletPointSplitter();
                }
            
                private void checkForBulletPointSplitter() {
                    String text = (String) getText();
                    if (text.contains(SPLITTER_CHAR)) {
                        injectBulletPoints(text);
                    }
                }
            
                private void injectBulletPoints(String text) {
                    String newLinedText = addNewLinesBetweenBullets(text);
                    String htmlBulletText = addBulletPoints(newLinedText);
                    setText(Html.fromHtml(htmlBulletText));
                }
            
                private String addNewLinesBetweenBullets(String text) {
                    String newLinedText = text.replace(SPLITTER_CHAR, NEWLINE_CHAR + SPLITTER_CHAR);
                    newLinedText = newLinedText.replaceFirst(NEWLINE_CHAR, "");
                    return newLinedText;
                }
            
                private String addBulletPoints(String newLinedText) {
                    return newLinedText.replace(SPLITTER_CHAR, HTML_BULLETPOINT);
                }
            
            }
            
            package.com;
            导入android.content.Context;
            导入android.text.Html;
            导入android.util.AttributeSet;
            导入android.widget.TextView;
            公共类BulletTextView扩展了TextView{
            私有静态最终字符串拆分器_CHAR=“--”;
            私有静态最终字符串换行字符=“
            ”; 私有静态最终字符串HTML_BULLETPOINT=“•;”; 公共公告文本视图(上下文、属性集属性){ 这(context、attrs、android.R.attr.textViewStyle); } 公共公告文本视图(上下文、属性集属性、int-defStyle){ 超级(上下文、属性、定义样式); checkForBulletPointSplitter(); } 私有void checkForBulletPointSplitter(){ String text=(String)getText(); if(text.contains(SPLITTER_CHAR)){ 补充要点(文本); } } 专用点(字符串文本){ 字符串newLinedText=addnewlinesbween项目符号(文本); 字符串htmlBulletText=addBulletPoints(newLinedText); setText(Html.fromHtml(htmlBulletText)); } 专用字符串AddNewLinesBeween项目符号(字符串文本){ 字符串newLinedText=text.replace(拆分器字符、换行符字符+拆分器字符); newLinedText=newLinedText.replaceFirst(换行字符“”); 返回newLinedText; } 私有字符串addBulletPoints(字符串newLinedText){ 返回newLinedText.replace(SPLITTER\u CHAR、HTML\u BULLETPOINT); } }
            支持缺少的HTML标记的另一种方法是很好地替换它们,如图所示

            使用简单的TextView和复合的drawable。比如说

            <TextView     
                android:text="Sample text"
                android:drawableLeft="@drawable/bulletimage" >
            </TextView>
            

            受这里各种答案的启发,我创建了一个实用程序类,使其成为一个简单的单行程序。这将为包装文本创建一个带有缩进的项目符号列表。 它具有组合字符串、字符串资源和字符串数组资源的方法

            它将创建一个可以传递给TextView的CharSequence。例如:

            CharSequence bulletedList = BulletListUtil.makeBulletList("First line", "Second line", "Really long third line that will wrap and indent properly.");
            textView.setText(bulletedList);
            
            希望对你有帮助。享受

            注意:这将使用系统标准项目符号,一个与文本颜色相同的小圆圈。如果您想要自定义弹头,请考虑子类并重写其<代码>拉引边缘()/代码>以绘制所需的子弹。请看一看,了解它是如何工作的

            public class BulletTextUtil {
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param context
             * @param stringArrayResId A resource id pointing to a string array. Each string will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletListFromStringArrayResource(int leadingMargin, Context context, int stringArrayResId) {
                return makeBulletList(leadingMargin, context.getResources().getStringArray(stringArrayResId));
            }
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param context
             * @param linesResIds An array of string resource ids. Each string will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletListFromStringResources(int leadingMargin, Context context, int... linesResIds) {
                int len = linesResIds.length;
                CharSequence[] cslines = new CharSequence[len];
                for (int i = 0; i < len; i++) {
                    cslines[i] = context.getString(linesResIds[i]);
                }
                return makeBulletList(leadingMargin, cslines);
            }
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param lines An array of CharSequences. Each CharSequences will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
                SpannableStringBuilder sb = new SpannableStringBuilder();
                for (int i = 0; i < lines.length; i++) {
                    CharSequence line = lines[i] + (i < lines.length-1 ? "\n" : "");
                    Spannable spannable = new SpannableString(line);
                    spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    sb.append(spannable);
                }
                return sb;
            }
            
            }
            
            公共类BulletTextUtil{
            /**
            *返回包含项目符号和正确缩进列表的字符序列。
            *
            *@param leadingMargin(像素),项目符号左边缘和文本左边缘之间的间距。
            *@param上下文
            *@param stringArrayResId指向字符串数组的资源id。每个
            
            package com.blundell;
            
            import android.content.Context;
            import android.text.Html;
            import android.util.AttributeSet;
            import android.widget.TextView;
            
            public class BulletTextView extends TextView {
                private static final String SPLITTER_CHAR = "--";
                private static final String NEWLINE_CHAR = "<br/>";
                private static final String HTML_BULLETPOINT = "&#8226;";
            
                public BulletTextView(Context context, AttributeSet attrs) {
                    this(context, attrs, android.R.attr.textViewStyle);
                }
            
                public BulletTextView(Context context, AttributeSet attrs, int defStyle) {
                    super(context, attrs, defStyle);
                    checkForBulletPointSplitter();
                }
            
                private void checkForBulletPointSplitter() {
                    String text = (String) getText();
                    if (text.contains(SPLITTER_CHAR)) {
                        injectBulletPoints(text);
                    }
                }
            
                private void injectBulletPoints(String text) {
                    String newLinedText = addNewLinesBetweenBullets(text);
                    String htmlBulletText = addBulletPoints(newLinedText);
                    setText(Html.fromHtml(htmlBulletText));
                }
            
                private String addNewLinesBetweenBullets(String text) {
                    String newLinedText = text.replace(SPLITTER_CHAR, NEWLINE_CHAR + SPLITTER_CHAR);
                    newLinedText = newLinedText.replaceFirst(NEWLINE_CHAR, "");
                    return newLinedText;
                }
            
                private String addBulletPoints(String newLinedText) {
                    return newLinedText.replace(SPLITTER_CHAR, HTML_BULLETPOINT);
                }
            
            }
            
            <TextView     
                android:text="Sample text"
                android:drawableLeft="@drawable/bulletimage" >
            </TextView>
            
            CharSequence bulletedList = BulletListUtil.makeBulletList("First line", "Second line", "Really long third line that will wrap and indent properly.");
            textView.setText(bulletedList);
            
            public class BulletTextUtil {
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param context
             * @param stringArrayResId A resource id pointing to a string array. Each string will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletListFromStringArrayResource(int leadingMargin, Context context, int stringArrayResId) {
                return makeBulletList(leadingMargin, context.getResources().getStringArray(stringArrayResId));
            }
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param context
             * @param linesResIds An array of string resource ids. Each string will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletListFromStringResources(int leadingMargin, Context context, int... linesResIds) {
                int len = linesResIds.length;
                CharSequence[] cslines = new CharSequence[len];
                for (int i = 0; i < len; i++) {
                    cslines[i] = context.getString(linesResIds[i]);
                }
                return makeBulletList(leadingMargin, cslines);
            }
            
            /**
             * Returns a CharSequence containing a bulleted and properly indented list.
             *
             * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
             * @param lines An array of CharSequences. Each CharSequences will be a separate line/bullet-point.
             * @return
             */
            public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
                SpannableStringBuilder sb = new SpannableStringBuilder();
                for (int i = 0; i < lines.length; i++) {
                    CharSequence line = lines[i] + (i < lines.length-1 ? "\n" : "");
                    Spannable spannable = new SpannableString(line);
                    spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    sb.append(spannable);
                }
                return sb;
            }
            
            }
            
                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
            
                    >
            
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="0.2"
                        android:text="1." />
            
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="3"
                        android:text="@string/help_points1" />
                </TableRow>
            
                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    >
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="0.2"
                        android:text="2." />
            
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="3"
                        android:text="@string/help_points2" />
                </TableRow>
                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    >
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="0.2"
                        android:text="3." />
                    <TextView
                        style="@style/helpPagePointsStyle"
                        android:layout_weight="3"
                        android:text="@string/help_points3" />
                </TableRow>
            
            
            </TableLayout>
            
            <style name="helpPagePointsStyle">
                <item name="android:layout_width">0dp</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:gravity">left</item>
            </style>
            
            <string name="bullet_ed_list">\n\u2022 He has been Chairman of CFL Manufacturers Committee of ELCOMA, the All India Association of Lighting Equipment Manufacturers.
            \n\u2022 He has been the President of Federation of Industries of India (FII).</string>
            
            fun List<String>.toBulletedList(): CharSequence {
                return SpannableString(this.joinToString("\n")).apply {
                    this@toBulletedList.foldIndexed(0) { index, acc, span ->
                        val end = acc + span.length + if (index != this@toBulletedList.size - 1) 1 else 0
                        this.setSpan(BulletSpan(16), acc, end, 0)
                        end
                    }
                }
            }
            
            val bulletedList = listOf("One", "Two", "Three").toBulletedList()
            label.text = bulletedList
            
            package com.fbs.archBase.ui.spans
            
            import android.graphics.Canvas
            import android.graphics.Color
            import android.graphics.Paint
            import android.text.Layout
            import android.text.Spanned
            import android.text.style.LeadingMarginSpan
            import androidx.annotation.ColorInt
            
            class CustomBulletSpan(
                    private val bulletRadius: Int = STANDARD_BULLET_RADIUS,
                    private val gapWidth: Int = STANDARD_GAP_WIDTH,
                    @ColorInt private val circleColor: Int = STANDARD_COLOR
            ) : LeadingMarginSpan {
            
                private companion object {
                    val STANDARD_BULLET_RADIUS = Screen.dp(2)
                    val STANDARD_GAP_WIDTH = Screen.dp(8)
                    const val STANDARD_COLOR = Color.BLACK
                }
            
                private val circlePaint = Paint().apply {
                color = circleColor
                    style = Paint.Style.FILL
                    isAntiAlias = true
                }
            
                override fun getLeadingMargin(first: Boolean): Int {
                    return 2 * bulletRadius + gapWidth
                }
            
                override fun drawLeadingMargin(
                        canvas: Canvas, paint: Paint, x: Int, dir: Int,
                        top: Int, baseline: Int, bottom: Int,
                        text: CharSequence, start: Int, end: Int,
                        first: Boolean,
                        layout: Layout?
                ) {
                    if ((text as Spanned).getSpanStart(this) == start) {
                        val yPosition = (top + bottom) / 2f
                        val xPosition = (x + dir * bulletRadius).toFloat()
            
                        canvas.drawCircle(xPosition, yPosition, bulletRadius.toFloat(), circlePaint)
                    }
                }
            }
            
                       EditText  edtNoteContent = findViewById(R.id.editText_description_note);            
            
                    edtNoteContent.addTextChangedListener(new TextWatcher(){
                        @Override
                        public void afterTextChanged(Editable e) {
            
                        }
                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            
                        }
                        @Override
                        public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
                        {
                            if (lengthAfter > lengthBefore) {
                                if (text.toString().length() == 1) {
                                    text = "◎ " + text;
                                    edtNoteContent.setText(text);
                                    edtNoteContent.setSelection(edtNoteContent.getText().length());
                                }
                                if (text.toString().endsWith("\n")) {
                                    text = text.toString().replace("\n", "\n◎ ");
                                    text = text.toString().replace("◎ ◎", "◎");
                                    edtNoteContent.setText(text);
                                    edtNoteContent.setSelection(edtNoteContent.getText().length());
                                }
                            }
                        }
                    });
            
            <TextView
                android:id="@+id/txtData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/draw_bullet_list"
                android:drawablePadding="@dimen/padding_8dp"
                android:text="Hello"
                android:textColor="@color/colorBlack" />
            
            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval">
                <solid android:color="@color/colorAccent" />
            
                <size
                    android:width="12dp"
                    android:height="12dp" />
            
            </shape>