English 中文(简体)
案文 . . . . . . .
原标题:Set TextView text from html-formatted string resource in XML

我在<条码>中有一些固定体,例如:

<resources>
    <string name="somestring">
        <B>Title</B><BR/>
        Content
    </string>
</resources>

和在我布局中,我用“html”的座右铭填了。

<TextView android:id="@+id/formattedtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/htmlstring"/>

如果我这样做,<代码>格式<>格式>格式>的内容只是<代码>部分的内容。 any掉了任何html的标签,因此没有格式。

我知道,有可能以方案方式确定格式案文。

.setText(Html. FromHtml (somestring));

因为在我方案的其他部分,我利用了这一手段,而现在,我正在按照预期开展工作。

为了说明这一职能,我需要一个<代码>的代用代码<>,但此时我的布局只是一个简单或少的静态观点,而我更喜欢这样去做,以免我产生一个<条码>的代用代码<>。

我看一看什么是显而易见的? 根本不可能? 任何帮助或工作方式都受到欢迎!

Edit: 就一些事情进行了审判,似乎以Xml语进行超文本格式的做法有一些限制:

  • 标签必须写上下级文件

  • some tags which are mentioned here do not work, e.g. <br/> (it s possible to use instead)

最佳回答

就在任何人发现这种情况时,还有一种没有记录的更恶性的替代物(在搜查小时后,我乘车,最后在安康克的灯塔名单上找到)。 页: 1

<![CDATA[ ...raw html... ]]>

Edge Cases:

  • Characters like apostrophe ( ), double-quote ("), and ampersand (&) only need to be escaped if you want them to appear in the rendered text AS themselves, but they COULD be plausibly interpreted as HTML.
    • and " can be represented as and ", or &apos; and &quot;.
    • < and > always need to be escaped as &lt; and &gt; if you literally want them to render as < and > in the text.
    • Ampersand (&) is a little more complicated.
      • Ampersand followed by whitespace renders as ampersand.
      • Ampersand followed by one or more characters that don t form a valid HTML entity code render as Ampersand followed by those characters. So... &qqq; renders as &qqq;, but &lt;1 renders as <1.

例:

<string name="nice_html">
<![CDATA[
<p>This is a html-formatted "string" with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph from the same  string .</p>
<p>To be clear, 0 &lt; 1, & 10 &gt; 1<p>
]]>
</string>

之后,在你的法典中:

TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html), FROM_HTML_MODE_LEGACY));

IMHO,这是与以下机构合作的几条大nic子:


< August 2021 Update: 我的原始答复使用了Html(String)的Html,该词在AP24中作了解释。 替代案文是:

目 录 也许会奏效......但其他国旗之一可能是你们想要做的更好的选择。

最后,如果你更喜欢使用<Markdown syntax而不是超文本”的案文,那么现在有多个第三方图书馆可以方便使用,其中包括

问题回答

www.un.org/Depts/DGACM/index_spanish.htm 由于这里的最高答案是提出一些错误的(或至少过于复杂),我认为应该加以更新,尽管问题已经存在:

在使用安伯斯山的强力资源时,你只得从getString(......)上调。 Java Code or use android:text="@string/. > > /code> in their presentout XML.

www.un.org/Depts/DGACM/index_spanish.htm 即便是你想要在您中使用超文本标记 您不必做很多改动:。

你们在努力中唯一需要依靠的是:

  • double quotation mark: " becomes "
  • single quotation mark: becomes
  • ampersand: & becomes &#38; or &amp;

这就是说,你可以增加<>。 不含的超文本标记 标签:

<string name="my_string"><b>Hello World!</b> This is an example.</string>

不过,当然,你只应当使用<代码><b>,<i><u>,因为它们列在文件中。

如果您希望使用<从XML到<>的超文本指示,则仅使用android:text="@string/. ,则该指示将予以罚款。

唯一的区别是,如果你想使用 Java代码<>上的超文本,那么你现在必须使用getText(......)而不是getString(......),因为前者保持风格,后者只会将其 strip。

它很容易。 CDATA, no Html.fromHtml(......)

如果你did在超文本标记中标明你的特性,你将只需要Html. fromHtml(......)。 然后使用getString(......)。 如果你希望通过“强令”至<条码>,则可能需要这样做。

http://developer.android.com/guide/topics/resources/string-resource.html rel=“noreferer”也载于《

<><>Edit>:

<代码>getText(......)与一架替补的超文本(如I ve 拟议)或CDATA各节和Html. FromHtml(......)之间没有区别。

比较图如下:

“entergraph

页: 1

<resources>
    <string name="somestring">
        &lt;B&gt;Title&lt;/B&gt;&lt;BR/&gt;
        Content
    </string>
</resources>

Android没有说明资源类型(如案文/解释或案文/html)。 然而,目前的工作是使开发商能够在XML档案中说明这一点。

  1. Define a custom attribute to specify that the android:text attribute is html.
  2. Use a subclassed TextView.

一旦确定这些内容,你就可以在Xml文档中以超文本表示,而不必再打电话。 我感到相当惊讶的是,这种做法不是APIC的一部分。

这一解决办法的用意是,安乐施室模拟器将显示作为超文本的文本。

“在座的影像描述”/

res/values/strings.xml (the string resource as HTML)

<resources>
<string name="app_name">TextViewEx</string>
<string name="string_with_html"><![CDATA[
       <em>Hello</em> <strong>World</strong>!
 ]]></string>
</resources>

layout.xml (only the relevant parts)

宣示习俗赋予名称空间,并添加 and-ex:isHtml属性。 并使用案文次等。

<RelativeLayout
...
xmlns:android_ex="http://schemas.android.com/apk/res-auto"
...>

<tv.twelvetone.samples.textviewex.TextViewEx
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_with_html"
    android_ex:isHtml="true"
    />
 </RelativeLayout>

res/values/attrs.xml (define the custom attributes for the subclass)

 <resources>
<declare-styleable name="TextViewEx">
    <attr name="isHtml" format="boolean"/>
    <attr name="android:text" />
</declare-styleable>
</resources>

TextViewEx.java (the subclass of TextView)

 package tv.twelvetone.samples.textviewex;

 import android.content.Context;
 import android.content.res.TypedArray;
 import android.support.annotation.Nullable;
 import android.text.Html;
 import android.util.AttributeSet;
 import android.widget.TextView;

public TextViewEx(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewEx, 0, 0);
    try {
        boolean isHtml = a.getBoolean(R.styleable.TextViewEx_isHtml, false);
        if (isHtml) {
            String text = a.getString(R.styleable.TextViewEx_android_text);
            if (text != null) {
                setText(Html.fromHtml(text));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        a.recycle();
    }
}
}

最新信息:

Html. FromHtml (string);/deprecated after Anders N edition.

遵循守则支持甲型六氯环己烷和乙型六氯环己烷及其以上版本......

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(yourHtmlString,Html.FROM_HTML_MODE_LEGACY));
}

else 
{
textView.setText(Html.fromHtml(yourHtmlString));
}
  String termsOfCondition="<font color=#cc0029>Terms of Use </font>";
  String commma="<font color=#000000>, </font>";
  String privacyPolicy="<font color=#cc0029>Privacy Policy </font>";
  Spanned text=Html.fromHtml("I am of legal age and I have read, understood, agreed and accepted the "+termsOfCondition+commma+privacyPolicy);
        secondCheckBox.setText(text);

我还有一个案例,即我没有机会将CDATA纳入Xml,因为我从一个服务器那里获得软射超。

这里是我从服务器获得的:

<p>The quick brown&nbsp;<br />
fox jumps&nbsp;<br />
 over the lazy dog<br />
</p>

这似乎更为复杂,但解决办法更为简单。

private TextView textView;

protected void onCreate(Bundle savedInstanceState) { 
.....
textView = (TextView) findViewById(R.id.text); //need to define in your layout
String htmlFromServer = getHTMLContentFromAServer(); 
textView.setText(Html.fromHtml(htmlFromServer).toString());

}

Hope it helps!
Linh

If you want to show html scrip in android app Like TextView

请遵循本守则

var stringvalue = "Your Sting"

yourTextVew.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT)
        } else {
            Html.fromHtml(stringvalue)
        }

String stringvalue = "Your String";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                   yourTextVew.setText(Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT))
                } else {
                   yourTextVew.setText( Html.fromHtml(stringvalue))
                }




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签