【问题标题】:How to show colour code bullet for each string如何显示每个字符串的颜色代码项目符号
【发布时间】:2017-11-01 20:54:32
【问题描述】:

这就是我使用 Unicode 字符显示字符串的项目符号列表(最多 4 个项目)的方式,现在我需要将项目符号颜色编码为红色,并且我不想将文本的第二行开始到下面要点

我尝试使用 drwableLeft 添加 textView 但项目符号与标题的第一行不相关

android:drawablePadding="2dp" 
android:drawableLeft="@drawable/ic_red_bullet"

有什么方法可以添加带有彩色项目符号的文本

<string name="headlines">
\n\u25CF Conversion disorder - the mysterious condition dogged by doubt and stigma \n
\n\u25CF Baseless and ludicrous: Kremlin responds to Mueller probe \n
\n\u25CF Ibrahims back in court over alleged drug and tobacco imports \n
</string>


    Layout-file

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">




        <TextView
            android:id="@+id/textView2"
            android:layout_width="356dp"
            android:layout_height="wrap_content"
            android:text="@string/headlines"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintTop_creator="1"
            android:textColor="@color/colorBlack"
            tools:layout_constraintRight_creator="1"
            android:layout_marginRight="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginTop="28dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_bias="1.0" />

    </android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 所以你只想改变项目符号的颜色而不是它后面的文本,对吧?

标签: android textview


【解决方案1】:

您可以为此目的使用 Spannable (SpannableString)。看到那个话题Set color of TextView span in Android

【讨论】:

    【解决方案2】:

    将此代码放入您的活动中...

      TextView TV = (TextView) findViewById(R.id.textView2);
    
        Spannable wordtoSpan = new SpannableString(" \u25CF Conversion disorder - the mysterious condition dogged by doubt and stigma ");
    
        wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
        TV.setText(wordtoSpan);
    

    【讨论】:

    • 这仅适用于彩色项目符号,但对“我不想将文本的第二行开始到项目符号点下方”没有帮助
    【解决方案3】:

    最后,写一个复合视图。

        layout/bullet_textview.xml
    
        <?xml version="1.0" encoding="utf-8"?>
        <merge xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView
                android:id="@+id/bullet_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_fiber_manual_record_black_24dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                />
    
            <TextView
                android:id="@+id/data_text_view"
                android:paddingLeft="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/header1"
                android:textColor="@color/colorBlack"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="14sp" />
        </merge>
      =========  
        public class BulletTextView extends LinearLayout {
    
            private TextView dataText;
    
            String dataTextValue;
            public BulletTextView(Context context) {
                super(context);
                initializeViews(context);
            }
    
            public BulletTextView(Context context, @Nullable AttributeSet attrs) {
                super(context, attrs);
    
                TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
                dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);
    
                initializeViews(context);
            }
    
            public BulletTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
                dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);
                initializeViews(context);
            }
    
            public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
                super(context, attrs, defStyleAttr, defStyleRes);
                TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
                dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);
                initializeViews(context);
            }
    
            /**
             * Inflates the views in the layout.
             *
             * @param context
             *           the current context for the view.
             */
            private void initializeViews(Context context) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.bullet_textview, this);
            }
    
            @Override
            protected void onFinishInflate() {
                super.onFinishInflate();
                dataText = (TextView) this.findViewById(R.id.data_text_view);
                dataText.setText(dataTextValue);
            }
    
            public void setData(String  data){
                dataText.setText(data);
            }
        }
    
        values/attr.xml
    ===================
        <resources>
            <declare-styleable name="BulletTextView">
                <attr name="data" format="string" />
            </declare-styleable>
        </resources>
    
    
        and Usage
      ============  
        <LinearLayout
                    android:layout_width="368dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <yourpackage.BulletTextView
                        android:id="@+id/firstHeader"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:orientation="horizontal" />
    
                    <yourpackage.BulletTextView
                        android:id="@+id/secondHeader"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:orientation="horizontal"
                        bullettext:data="@string/title_dashboard" />
    
                    <yourpackage.BulletTextView
                        android:id="@+id/thirdHeader"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:orientation="horizontal"
                        bullettext:data="@string/headlinesnow" />
                </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2020-10-23
      • 2011-09-12
      相关资源
      最近更新 更多