Android 可扩展文本在对话框片段中不起作用

Android 可扩展文本在对话框片段中不起作用,android,textview,android-dialogfragment,spannablestring,Android,Textview,Android Dialogfragment,Spannablestring,我在我的对话框中添加了TextView,并在TextView中动态显示Spannable数据。但是它不适用于文本 下面是我生成三个可扩展字符串并添加到TextView的代码 TextView textViewTicketSummary = (TextView) dialog.findViewById(R.id.ticketSummaryTextView); SpannableStringBuilder summary = new SpannableStringBuilder();

我在我的
对话框中添加了
TextView
,并在
TextView
中动态显示Spannable数据。但是它不适用于文本

下面是我生成三个可扩展字符串并添加到TextView的代码

 TextView  textViewTicketSummary = (TextView) dialog.findViewById(R.id.ticketSummaryTextView);
 SpannableStringBuilder summary = new SpannableStringBuilder();
    SpannableString VehicleCaptureSpan, TotalVehicleSpan, DurationTimeSpan;

    String VehicleCapture = "Total Vehicles Captured:9";
    VehicleCaptureSpan = new SpannableString(VehicleCapture);
    VehicleCaptureSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, VehicleCapture.length(), 0);
    VehicleCaptureSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, VehicleCapture.length(), 0);
    VehicleCaptureSpan.setSpan(new RelativeSizeSpan(1.5f), 0, VehicleCapture.length(), 0);

    String TotalVehicle = "Total Car Park Capacity:10 ";
    TotalVehicleSpan = new SpannableString(TotalVehicle);
    TotalVehicleSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, TotalVehicle.length(), 0);
    TotalVehicleSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, TotalVehicle.length(), 0);
    TotalVehicleSpan.setSpan(new RelativeSizeSpan(1.5f), 0, TotalVehicle.length(), 0);

    String DurationTime = "Total Duration: 0 Min 3 Secs ";
    DurationTimeSpan = new SpannableString(DurationTime);
    DurationTimeSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, DurationTime.length(), 0);
    DurationTimeSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, DurationTime.length(), 0);
    DurationTimeSpan.setSpan(new RelativeSizeSpan(1.5f), 0, DurationTime.length(), 0);

    summary.append(VehicleCapture).append("\n\n")
            .append(TotalVehicle).append("\n\n")
            .append(DurationTime).append("\n");

    textViewTicketSummary.setText(summary, TextView.BufferType.SPANNABLE);
以下是我的布局:

以下是输出:


请给我一些提示。为什么它在
对话框片段中不起作用?

要实现这一点,您应该指定
Span
的类型。您可以在
setSpan()
方法的第四个参数中进行设置。在您的情况下,该值应为:

SPAN_EXCLUSIVE_EXCLUSIVE类型的跨距不展开以包括在其起点或终点插入的文本。它们的长度永远不能为0,如果它们覆盖的所有文本都被删除,它们将自动从缓冲区中删除

像这样:

VehicleCaptureSpan.setSpan(
        new ForegroundColorSpan(Color.RED),
        0,
        VehicleCapture.length(),
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
但是,如果要设置整个文本的颜色或文本样式,可以使用
TextView
参数设置它们:

textViewTicketSummary.setTextColor(Color.RED);
textViewTicketSummary.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
textViewTicketSummary.setTypeface(Typeface.DEFAULT_BOLD);
UPD: 您的代码中有一个很有趣的小错误。您试图将
String
s而不是
SpannableString
值附加到生成器。只需将该代码更改为以下代码:

summary.append(VehicleCaptureSpan).append("\n\n")
        .append(TotalVehicleSpan).append("\n\n")
        .append(DurationTimeSpan).append("\n");

为什么要使用
0
标志(
setSpan()
)的最后一个参数?您可能应该使用预定义的标志之一。据我所知,您应该使用
Spannable.SPAN\u EXCLUSIVE\u EXCLUSIVE
@shoerat好吧,让我试试……summary.append(VehicleCapture).append(“\n\n”).append(TotalVehicle).append(“\n\n”).append(DurationTime).append(“\n”);必须是摘要.append(VehicleCaptureSpan).append(“\n\n”).append(TotalVehicleSpan).append(“\n\n”).append(DurationTimeSpan).append(“\n”);我明天会尝试您的解决方案。@MD我发现您的代码中有一个小错误。请查看我的更新答案。
summary.append(VehicleCaptureSpan).append("\n\n")
        .append(TotalVehicleSpan).append("\n\n")
        .append(DurationTimeSpan).append("\n");