Spannable Text在Dialog Fragment中不起作用

时间:2014-11-24 13:02:31

标签: android textview android-dialogfragment spannablestring

我已在TextView片段中添加Dialog,并在此TextView中动态显示可跨越数据。但它不会对文本应用 Spannable 效果。

这是我的代码,用于生成Three Spannable String并添加到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);

这是我的布局:

<?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:layout_gravity="center"
android:background="@drawable/shape_dialog_patrol"
android:orientation="vertical"
android:paddingBottom="8dp" >

 <TextView
    android:id="@+id/dialogOneButtonMessage"
    style="@style/OneButtonDialogBody"
    android:text="Dialog Body" />

 <TextView
    android:id="@+id/ticketSummaryTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/shape_textview_rounded_lightblack"
    android:lineSpacingExtra="4dp"
    android:padding="12dp"
    android:text="Ticket Details"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white"/>

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal|top"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/dialogOneButtonText"
        style="@style/OneButtonDialogButton2"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_horizontal"
        android:text="@string/ok" />
   </RelativeLayout>

 </LinearLayout>

这是输出:

enter image description here

请给我一些提示..为什么它在Dialog Fragment无效?

1 个答案:

答案 0 :(得分:4)

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

  

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: 你的代码中有一个小的有趣的bug。您正尝试将String s而不是SpannableString值附加到构建器。只需将该代码更改为此代码:

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