草庐IT

android - 可跨文本在对话框 fragment 中不起作用

coder 2023-12-03 原文

我在我的 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/

有关android - 可跨文本在对话框 fragment 中不起作用的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  3. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  4. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  5. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  6. ruby-on-rails - rspec - 我怎样才能让 "pendings"有我的文本而不仅仅是 "No reason given" - 2

    我有这个代码:context"Visitingtheusers#indexpage."dobefore(:each){visitusers_path}subject{page}pending('iii'){shouldhave_no_css('table#users')}pending{shouldhavecontent('Youhavereachedthispageduetoapermissionic错误')}它会导致几个待处理,例如ManagingUsersGivenapractitionerloggedin.Visitingtheusers#indexpage.#Noreason

  7. ruby - 如何为 pbcopy 生成富文本链接 - 2

    我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te

  8. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

  9. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  10. ruby-on-rails - 尝试打开 .gitignore 以在文本编辑器中对其进行编辑,但在 OS X Mountain Lion 上找不到文件位置 - 2

    我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位

随机推荐