我在我的 Dialog
fragment 中添加了 TextView
并且我在此 TextView
中动态显示 Spannable 数据。但它不会对文本应用 Spannable 效果。
这是我生成三个 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);
这是我的布局:
<?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>
这是输出:
请给我一些提示..为什么它在 Dialog Fragment
中不起作用?
最佳答案
要实现这一点,您应该指定 Span
的类型。您可以在 setSpan()
方法的第四个参数中设置它。在您的情况下,该值应为 SPAN_EXCLUSIVE_EXCLUSIVE :
Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
像这样:
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);
更新:
您的代码中有一个有趣的小错误。您正在尝试将 String
而不是 SpannableString
值附加到生成器。只需将该代码更改为以下代码即可:
summary.append(VehicleCaptureSpan).append("\n\n")
.append(TotalVehicleSpan).append("\n\n")
.append(DurationTimeSpan).append("\n");
关于android - 可跨文本在对话框 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105499/