【问题标题】:How do I add a bullet symbol in TextView?如何在 TextView 中添加项目符号?
【发布时间】:2011-03-26 15:58:12
【问题描述】:

我有一个 TextView,我想通过 XML 在我的文本中添加一个项目符号。有可能吗?

【问题讨论】:

    标签: android android-layout special-characters textview


    【解决方案1】:

    您必须使用正确的character encoding 才能完成此效果。你可以试试•


    更新

    澄清一下:使用setText("\u2022 Bullet"); 以编程方式添加项目符号。 0x2022 = 8226

    【讨论】:

    • This 帮助了我。
    • 这是正确的答案。比粘贴子弹更正确。
    • @Benny,如果我以编程方式设置文本,这将不起作用。 textView.setText("• 你好");
    • 澄清一下:使用setText("\u2022 Bullet"); 以编程方式添加项目符号。 0x2022 = 8226
    • 这里是这些不同风格子弹的字符代码:• = \u2022, ● = \u25CF, ○ = \u25CB, ▪ = \u25AA, ■ = \u25A0, □ = \u25A1, ► = \u25BA
    【解决方案2】:

    这对我有用:

    <string name="text_with_bullet">Text with a \u2022</string>
    

    【讨论】:

      【解决方案3】:

      复制粘贴: •。我已经用其他奇怪的字符完成了它,例如 ◄ 和 ►。

      编辑: here 就是一个例子。底部的两个Buttons 有android:text="◄""►"

      【讨论】:

      • 问题出在换行时。它不会缩进第二行
      • 只使用水平方向的线性布局,第一个带有“图标和空间”的文本视图第二个:=) 文本,=> 全部预期
      【解决方案4】:

      Prolly 有一个更好的解决方案,但这就是我所做的。

      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              >
              <TableRow>    
                  <TextView
                      android:layout_column="1"
                      android:text="•"></TextView>
                  <TextView
                      android:layout_column="2"
                      android:layout_width="wrap_content"
                      android:text="First line"></TextView>
              </TableRow>
              <TableRow>    
                  <TextView
                      android:layout_column="1"
                      android:text="•"></TextView>
                  <TextView
                      android:layout_column="2"
                      android:layout_width="wrap_content"
                      android:text="Second line"></TextView>
              </TableRow>
        </TableLayout>
      

      它的工作方式如你所愿,但确实是一种解决方法。

      【讨论】:

        【解决方案5】:

        您可以按照 Android 文档中的说明尝试 BulletSpan

        SpannableString string = new SpannableString("Text with\nBullet point");
        string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        

        【讨论】:

        • 在 API 28 之前使用 bulletRadius 属性怎么样?
        • 有什么方法可以设置子弹大小?
        • @UsmanRana 是的,你可以尝试用你喜欢的大小替换 20,即 BulletSpan 构造函数中的第三个参数,它代表半径。 BulletSpan(int gapWidth, int color, int bulletRadius)
        【解决方案6】:

        这就是我最终这样做的方式。

         <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
        
                    <View
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/circle"
                        android:drawableStart="@drawable/ic_bullet_point" />
        
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="Your text"
                        android:textColor="#000000"
                        android:textSize="14sp" />
                </LinearLayout>
        

        drawbale/circle.xml 的代码是

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:innerRadius="0dp"
          android:shape="ring"
          android:thickness="5dp"
          android:useLevel="false">
        
         <solid android:color="@color/black1" />
        
        </shape>
        

        【讨论】:

          【解决方案7】:

          使用 Unicode 我们可以很容易地做到这一点,但是如果想改变子弹的颜色,我尝试使用彩色子弹图像并将其设置为可绘制的左侧并且它起作用了

          <TextView     
              android:text="Hello bullet"
              android:drawableLeft="@drawable/bulleticon" >
          </TextView>
          

          【讨论】:

            【解决方案8】:

            在任何文本视图中添加项目符号的另一种最佳方法如下两步所述:

            首先,创建一个可绘制对象

            <?xml version="1.0" encoding="utf-8"?>
            <shape
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval">
            
                <!--set color of the bullet-->
               <solid 
                   android:color="#666666"/> //set color of bullet
            
                <!--set size of the bullet-->
               <size 
                   android:width="120dp"
                    android:height="120dp"/>
            </shape>
            

            然后在textview中添加这个drawable并使用下面的属性设置它的pedding

            android:drawableStart="@drawable/bullet"
            android:drawablePadding="10dp"
            

            【讨论】:

            • 非常有帮助,谢谢。
            【解决方案9】:

            由于 android 不支持&lt;ol&gt;, &lt;ul&gt; or &lt;li&gt; html 元素,我不得不这样做

            <string name="names"><![CDATA[<p><h2>List of Names:</h2></p><p>&#8226;name1<br />&#8226;name2<br /></p>]]></string>
            

            如果您想维护自定义空间,请使用&lt;/pre&gt; tag

            【讨论】:

              【解决方案10】:

              (几乎)所有选项都与使用html 标签有关。

              你可以为你的 TextView 使用 drawables如果它只有一行文本。

              类似这样的:

              <TextView
                          android:id="@+id/tv_with_bullet"
                          android:layout_width="match_parent"
                          android:layout_height="50dp"
                          app:drawableStartCompat="@drawable/ic_desired_bullet_icon" />
              
              

              并在 SVG 中添加所需的可绘制项目符号。它实际上不占用空间,让您无需添加复杂的string literals。您也可以在 here 中下载 SVG 文件以获取要点

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-08-06
                • 2021-07-25
                • 1970-01-01
                • 1970-01-01
                • 2017-01-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多