【问题标题】:changing a string in res/value/string via an activity in android通过 android 中的活动更改 res/value/string 中的字符串
【发布时间】:2014-10-16 09:47:33
【问题描述】:

我正在开发一个项目,我需要更改存储在 res/values 文件夹中的字符串。我在 google 和 stackoverflow 中搜索了很多,但找不到任何答案。 目标是我的项目中有一个特定的 xml 文件,其中包含另一个 xml 文件,其中 xml 的根保存在字符串中。喜欢:

<include layout="@string/xmlName"/>

<string name="xmlName">@layout/form2</string>

我想更改此字符串以更改我的布局视图。有什么有用的方法吗? tnx 提前

【问题讨论】:

  • 您必须将布局传递给&lt;include&gt;
  • 但这行代码运行良好!
  • i need to change a string stored in res/values folder.不能这样做以编程方式。
  • @Funkystein 那么还有其他方法可以在不使用活动的情况下更改布局吗?还是将我的字符串保存在其他地方以使其成为可能?
  • 您可以将字符串存储在数据库中并进行更改。或者将您的字符串加载到存储中。

标签: android xml string


【解决方案1】:
<include layout="@string/xmlName"/>

这不是通过更改字符串来更改视图的正确方法。更何况你不应该像这样给出布局ID。

在运行时,您必须将新视图膨胀到包含标签。

<include 
android:id="@+id/my_include_view"
layout="@layout/xyz_layout"/>

View view = findViewById(R.id.my_include_view);
view .removeAllViews();


LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
view .addView(layoutInflater.inflate(R.layout.new_layout, this, false) ); 

【讨论】:

  • 但我的 Eclipse 无法定义 getSystemService 方法。我该怎么办?
  • 你是在activity还是fragment中调用它
  • 我在活动中调用它
  • LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);试试这个
猜你喜欢
  • 1970-01-01
  • 2014-06-02
  • 2012-01-16
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 2016-06-19
  • 2012-06-28
相关资源
最近更新 更多