2016-06-08 60 views
1

我試圖把@string/string_b裏面的html <p></p>。例如,如何在html段落中添加@string?

<string name="string_a">very long text</string> 

<string name="string_b"> 
    <![CDATA[ 
     <body> 
      <p>This is string B</p> 
      <p>@string/string_a</p> 
      <p>Another string here</p> 
     </body> 
    ]]> 
</string> 

這將只顯示:
這串B
@字符串/ string_a
這裏的另一個字符串

但是我想表明:
這串B
非常長的文字。
另一個字符串在這裏

順便說一句,它會顯示在HtmlTextView。如果可以,我只想在xml裏面做。感謝你們!

+0

你管理,使其工作? – Vucko

+1

是的,但以另一種方式。通過擴展HtmlTextView。謝謝btw。 – Tuss

+0

沒問題,然後自己發佈答案,如果其他人有同樣的問題,他們可以看到解決方案。 – Vucko

回答

1

讓你strings.xml這種變化:

<string name="string_b"> 
     <![CDATA[ 
      <body> 
       <p>This is string B</p> 
       <p>%1$s</p> 
       <p>Another string here</p> 
      </body> 
     ]]> 
    </string> 

現在在你的Java代碼,你可以這樣做:

Resources res = getResources(); 
String string_a = res.getString(R.string.string_a); 
String string_b = String.format(res.getString(R.string.string_b), string_a); 

根據您的要求做的只是在XML中,我發現這question指的是同樣的事情。所以你可以看看那裏給出的答案,看看它是否適合你。

+0

是否有可能使它只在xml中? – Tuss

+0

查看問題及其答案,即我在編輯中鏈接的問題。 – Vucko

0

這是我的答案。希望它能幫助別人。擴展HtmlTextView爲我們提供了使用string格式添加新屬性的機會。然後,android:textcustom:text2可以在定製HtmlTextView內以編程方式連接。

1.如果您使用的是HtmlTextView 或TextView,請擴展它。

public class HtmlTextview extends HtmlTextView { 
    public HtmlTextview(Context context) { 
     super(context); 
    } 

    public HtmlTextview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setupText(attrs); 
    } 

    public HtmlTextview(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setupText(attrs); 
    } 

    private void setupText(AttributeSet attrs) { 

     String htmlString = (String) this.getText(); 
     String text2; 

     TypedArray attributeValuesArray = getContext().obtainStyledAttributes(attrs, R.styleable.concatenate, 0, 0); 
     try{ 
      text2 = attributeValuesArray.getString(R.styleable.concatenate_text2); 
     } finally { 
      attributeValuesArray.recycle(); 
     } 

     if(text2 != null && text2.length() > 0){ 
      htmlString = htmlString + text2; 
      this.setHtmlFromString(htmlString, new HtmlTextView.RemoteImageGetter()); 
     } 
    } 
} 

2. HtmlTextview屬性

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <declare-styleable name="concatenate"> 
     <attr name="text2" format="string"/> 
    </declare-styleable> 

</resources> 

3.在string.xml資源

<resources> 
    <string name="app_name">HtmlTry</string> 

    <string name="string_a"> 
     <![CDATA[ 
     <body> 
      <p>very long text. Can also formatted in <font color="#f44336">Html.</font></p> 
     </body> 
     ]]> 
    </string> 

    <string name="string_b"> 
     <![CDATA[ 
     <body> 
      <p>This is string B</p> 
     </body> 
     ]]> 
    </string> 

</resources> 

4.最後應用它的XML佈局內。

<com.htmltry.HtmlTextview 
    android:id="@+id/tv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/string_b" 
    custom:text2="@string/string_a" 
/> 

最終的結果是: enter image description here