Android:数字格式正确的 Spannable 字符串

Android: SpannableString with correctly formated numbers

我想要一个在枚举中正确编号的指令列表。为此,我使用 SpannableString 并且我有以下代码:

//Get string array from a sqLite Database
String [] instructions = MainActivity.sqLiteDataba.getPreparationInstructions(item);

//Create Spannable String Builder
int numberOfCurrentInstruction = 1;
SpannableStringBuilder builderInstructions =  new SpannableStringBuilder();
for (int i =0; i<instructions.length;i++) {
    if(instructions[i] == null ) {
        continue;
    }

    builderInstructions.append(numberOfCurrentInstruction + ". " + instructions[i] + "\n");
    numberOfCurrentInstruction++;
}

文本视图中的输出(在片段中调用)看起来不像所需的方式,如您在屏幕截图中所见:

我希望数字充当“要点”,这意味着如果 textView 中有换行符,则以下文本不应从数字开始,而应带有一点点 space。知道我该怎么做吗?

更新:我使用布局充气器尝试了下面发布的建议方法 (Darkman),但不幸的是只显示了第一个项目。这是片段中的代码:

    String [] instructions = MainActivity.sqLiteDB.getPreparationInstructions(item);

    
    int numberOfCurrentInstruction = 1;
    ViewGroup main = getView().findViewById(R.id.main);
    LayoutInflater inflator = getLayoutInflater();

    for (int i =0; i<instructions.length;i++) {
        if(instructions[i] == null ) {
            continue;

        }

        ViewGroup mainLayout = (ViewGroup) inflator.inflate(R.layout.custom, main, true);
        ViewGroup customLayout = (ViewGroup) mainLayout.getChildAt(i);


        TextView num = (TextView) customLayout.getChildAt(0);
        TextView text = (TextView) customLayout.getChildAt(1);
        num.setText((numberOfCurrentInstruction) + ".");
        text.setText(instructions[i]);
        numberOfCurrentInstruction++;
    }

这是 custom.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:gravity="right"
        android:layout_marginTop="@dimen/_2sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:textSize="@dimen/_7ssp"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="left"
        android:layout_marginTop="@dimen/_2sdp"
        android:layout_marginLeft="@dimen/_2sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:textSize="@dimen/_7ssp"/>
</LinearLayout>

并且 main.xml 在片段的 xml 文件中,看起来像这样:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/main">


    </LinearLayout>

数组instructions有5个元素,其中none为空,for循环执行了5次(我用LogTag检查过)。但是,仅显示第一条指令,而其他指令不显示。知道问题出在哪里吗?

如果使用fixed-space字体,在\n后加几个空格。 如果您使用等宽字体,添加空格不是最好的方法。在这种情况下,您可以添加符号“1”、“2”等而不是空格,并为此符号设置颜色,例如背景色。

您可以通过 XML 使用两个 TextView 来实现,如下例所示。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#333333"
    android:orientation="vertical"
    android:id="@+id/main">

</LinearLayout>

custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:gravity="right"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="left"
        android:layout_marginRight="10dp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity 
{
    private static final String[] str = {
        "Instruction 1: This is the first instruction that you have to do.",
        "Instruction 2: This is the second instruction that you have to do.",
        "Instruction 3: This is the third instruction that you have to do."
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        ViewGroup main = findViewById(R.id.main);
        LayoutInflater inflator = getLayoutInflater();
        
        for(int i=0; i<3; i++) {
            ViewGroup mainLayout = (ViewGroup) inflator.inflate(R.layout.custom, main, true);
            ViewGroup customLayout = (ViewGroup) mainLayout.getChildAt(i);
            TextView num = (TextView) customLayout.getChildAt(0);
            TextView text = (TextView) customLayout.getChildAt(1);
            num.setText((i+1) + ".");
            text.setText(str[i]);
        }
    }
}

输出: