2017-02-14 60 views
0

我在Android中有这样的代码;如何在对象中使用资源字符串? - Android

public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"}; 

我想要这样的代码;

public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"}; 

我试了太多的代码,但没有奏效。我能做些什么呢? 非常感谢。

回答

0

您需要使用Resources摆脱Android的resources.Try串下面

Resources res = context.getResources(); public static final String[] URLS = new String[]{res.getString(R.string.url_1), res.getString(R.string.url_2)};

1

@string资源被保存在R.java文件中的int引用中。所以,你不能直接使用它们。但是,还有另一种方法可以做到这一点。

您可以在arrays.xml文件中使用字符串数组。这将是...

<string-array name="urls"> 
    <item>https://www.url1.com/</item> 
    <item>https://www.url2.com/</item> 
    <item>https://www.url3.com/</item> 
</string-array> 

在您的Java代码中,您可以通过以下代码访问它。

public static final String[] URLS = context.getResources().getStringArray(R.array.urls); 
相关问题