Concatenate multiple strings in XML?

前端 未结 10 976
执笔经年
执笔经年 2020-12-01 05:09

I go through this How to concatenate multiple strings in android XML? and in the end there are comments that

For clarity, Its works:

&

相关标签:
10条回答
  • 2020-12-01 05:22

    How about this technique

    Use an array

    <string name="app_link">https://myapp.com/</string>
    
    <array name="update_dialog_msg">
        <item>Update can be downloaded manually by visiting this link:\n</item>
        <item>@string/app_link</item>
    </array>
    

    And then make a method in java class as below

    public String getStringArray(int resStringArray)
    {
        String str = "";
    
        String[] strArray = mContext.getResources().getStringArray(resStringArray);
        for (String s: strArray)
        {
            str += s;
        }
    
        return str;
    }
    

    Now call it from any java class as

    mDialog.setMessage(getStringArray(R.array.update_dialog_msg);
    // OR
    textView.setText(getStringArray(R.array.update_dialog_msg);
    
    0 讨论(0)
  • Yes, here is a complete way of doing this. Use data binding.

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
                        android:id="@+id/tv_one_month_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="16dp"
                        android:text="@{@string/string1 + @string/string2}"
                        />
    </layout>
    

    Here you can simply define your strings in strings.xml file.

    <string name="string1">string1 text</string>
    <string name="string2">string2 text</string>
    

    Obviously you have to enable data binding, here is the link for the same. https://developer.android.com/topic/libraries/data-binding/start

    0 讨论(0)
  • 2020-12-01 05:26

    You can concatenate the resources in gradle.build:

        resValue "string", "TWITTER_CALLBACK", "twitter_callback_" + applicationId
    
    0 讨论(0)
  • 2020-12-01 05:27

    No I don't think you can concatenate.

    <string name="aaa">aaa</string>
    <string name="bbb">bbb @string/aaa</string>
    

    Output - bbb @string/aaa

    If you do,

    <string name="aaa">aaa</string>
    <string name="bbb">@string/aaa bbb</string>  -> This won't work it
                                                          will give compilation error
    

    Because here it will search for a String with reference @string/aaa bbb which does not exists.

    Problem in your case was, you where using @strings/aaa which should be @string/aaa

    0 讨论(0)
  • 2020-12-01 05:31

    Yes, you can do it if your XML files are using DataBinding as you can see in this link

    Use it like this:

    "@{@string/first_string+ @string/second_string}"
    
    0 讨论(0)
  • 2020-12-01 05:33

    In XML only this is not possible but using the java code you can use the String.format() method.

    <string name="aaa">aaa</string>
    <string name="bbb">bbb %1$s</string>
    

    In java code

    String format = res.getString(R.string.bbb);
    String title = String.format(format, res.getString(R.string.aaa));
    

    So title will be a full string after concatenation of two strings.

    0 讨论(0)
提交回复
热议问题