在TextView中显示度数符号

Aka*_*eet 5 xml user-interface android

我希望在我的活动中显示50度,TextView如下所示

在此输入图像描述

我真的不知道如何做到这一点,通过谷歌搜索我找到了这个XML代码

android:text="50°"
Run Code Online (Sandbox Code Playgroud)

我不知道上面的代码是什么.
任何人都可以向我解释究竟是什么以及它是如何工作的.

All*_*n G 10

°只是将特殊字符插入 XML 的标准方法。您可以参考https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references以获得更完整的描述,但要分解:

  • & 表示它是一个特殊字符的开始。
  • #表示字符用数字表示(相对于特殊的字符串代码,例如&&符号)。
  • x 表示数字代码是十六进制值。
  • b0 度数符号的十六进制值(十进制为 176)。
  • ; 结束序列。

另一种表示是°使用十进制值而不是十六进制。

因此,如果您知道其 ASCII 十进制/十六进制值,则可以插入任何特殊字符。


Udi*_*hef 9

  1. 摄氏度使用:

        "\u2103" 
    
    Run Code Online (Sandbox Code Playgroud)

    华氏使用:

      "\u2109"
    
    Run Code Online (Sandbox Code Playgroud)

    仅用于没有c或f使用的度数符号:

     "\u00B0"
    
    Run Code Online (Sandbox Code Playgroud)

    例如:

      String TemperatureMeasurementStr = String.valueOf(measurement.getTemperature()) + "\u2103";
    
    Run Code Online (Sandbox Code Playgroud)

    或简单的例子:

      String TemperatureMeasurementStr = "37"+ "\u2103";
    
    Run Code Online (Sandbox Code Playgroud)

    并在textView中设置字符串

      public TextView temperatureTV;                     
      temperatureTV.setText(TemperatureMeasurementStr);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果你只想将符号添加到xml布局文件 - 只需使用:

      android:text="37\u2103"
    
    Run Code Online (Sandbox Code Playgroud)

例如:

<TextView
     android:id="@+id/temperature_measure"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignTop="@+id/temperature_icon"
     android:layout_marginBottom="-5dp"
     android:layout_marginLeft="10dp"
     android:layout_toRightOf="@+id/temperature_icon"
     android:text="37\u2103"
     android:textAlignment="center"
     android:textSize="50sp"
  />
Run Code Online (Sandbox Code Playgroud)
  1. 如果您希望此符号更小或颜色或样式不同,那么数字(当数字本身为"粗体"时,可以说"正常"),那么您可以使用SpannableString:

像这样:

String TemperatureMeasurementStr = String.valueOf(measurement.getTemperature()) + "\u2103";
SpannableString tempSpan=  new SpannableString(TemperatureMeasurementStr);
if (TemperatureMeasurement.length() >0){

    //the symbol will be smaller then the number
    tempSpan.setSpan(new RelativeSizeSpan(0.7f),TemperatureMeasurementStr.length() - 1, TemperatureMeasurementStr.length(), 0);

    //the number style will be bold and the symbol normal
   tempSpan.setSpan(new android.text.style.StyleSpan(Typeface.BOLD), 0,   TemperatureMeasurementStr.length()-1, 0);

    //the symbol color will be yellow
   tempSpan.setSpan(new ForegroundColorSpan(Color.YELLOW), TemperatureMeasurementStr.length() - 1, TemperatureMeasurementStr.length(), 0);
 }
 temperatureTV.setText(tempSpan);
Run Code Online (Sandbox Code Playgroud)


Stu*_*DTO 5

有很多方法可以做到:

1.- 您可以创建一个char如下:

char degreesymbol = '\u00B0';
Run Code Online (Sandbox Code Playgroud)

然后你可以把它添加到你的 TextView

2.- 你可以把它放在你XML的如下:

android:text="50&#x2103;"
Run Code Online (Sandbox Code Playgroud)

3.- 如果您想以编程方式进行操作,请执行以下操作:

YOURTEXTVIEW.setText((Your_Temperature) + " \u2109");
Run Code Online (Sandbox Code Playgroud)