我想完整地讀取位於src - > main - > res - > values下的string.xml文件。如何讀取位於值文件夾中的文件
爲了論證的緣故,我需要特定的文件來處理可能在我的應用程序中出現的一些本地化問題:說選定的設備語言是法語的,但仍然需要一些主英文字符串.xml文件僅適用於特定場景。因此,values/strings.xml中的所有資源都應存在於values-fr/strings.xml中,並將它們從值中移除-rr 不是選項。
- 顯然,那種
getString(R.string.text_name)
的解決方案是不能接受的,因爲我們會從values-fr/strings.xml
文件從values/strings.xml
收到的text_name
的價值,而不是。 這不會有所幫助:
getResources().getIdentifier("text_name", "string", getPackageName());
這不會有所幫助,因爲string.xml不是原始資產。
context.getResources().openRawResource(R.raw.test)
context.getAssets().open("values/string.xml");
這是我嘗試過,但沒有給我,我要找的文件:使用特定
Uri.parse("android.resource://" + context.getPackageName() + resource_id_for_string_file);
:
使用URI文件路徑,如:
File file = new File("/src/res/values/strings.xml"); File file = new File("/res/values/strings.xml");
使用this post(
class.getClass().getResourceAsStream(...)
)哪些也沒有工作。
老實說,雖然它會很容易,但我找不到任何地方解決這個問題。
你仍可以添加英文值對於法國文件中的某些鍵? – Yazan
您的方法不正確。如果你想從'values/strings.xml'的'values-fr/strings.xml'和其他值中讀取一些值,那麼就不要在values/strings中添加你需要的值。 xml「轉換爲'values-fr/strings.xml'。如果沒有特定語言環境的值,則將使用默認語言環境。 –
謝謝,但它並沒有幫助我,因爲我需要fr-fr字符串中的所有資源(我希望特定的場景不要訪問該文件並轉到values/strings.xml文件) – Dus