我想我做很多别的事情一样setText
上的小部件之前,从xml
文件中读取字符串,所以我该怎么办,没有一个活动对象调用getResources()
吗?如何从静态上下文获取资源内容?
回答
- 创建的
Application
一个子类,例如public class App extends Application {
- 坐落在
AndroidManifest.xml
您<application>
标签的android:name
属性指向你的新类,例如android:name=".App"
- 在您的应用实例的
onCreate()
方法中,将您的上下文(例如this
)保存到名为mContext
的静态字段中,并创建一个返回此字段的静态方法。getContext()
:
这是它应该如何看:
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
现在你可以使用:App.getContext()
每当你想获得一个上下文,然后getResources()
(或App.getContext().getResources()
)。
使用
Resources.getSystem().getString(android.R.string.cancel)
你可以在你的应用程序在任何地方使用它们,即使是在静态常量声明! 但仅限于系统资源!
我想,更多的方式是可行的。 但有时候,我使用这个解决方案。 (全全球):
import android.content.Context;
import <your package>.R;
public class XmlVar {
private XmlVar() {
}
private static String _write_success;
public static String write_success() {
return _write_success;
}
public static void Init(Context c) {
_write_success = c.getResources().getString(R.string.write_success);
}
}
//After activity created:
cont = this.getApplicationContext();
XmlVar.Init(cont);
//And use everywhere
XmlVar.write_success();
在你的类,在这里实现静态功能,可以调用一个私人\公共从这个类的方法。私人\ public方法可以访问getResources。
例如:
public class Text {
public static void setColor(EditText et) {
et.resetColor(); // it works
// ERROR
et.setTextColor(getResources().getColor(R.color.Black)); // ERROR
}
// set the color to be black when reset
private void resetColor() {
setTextColor(getResources().getColor(R.color.Black));
}
}
,并从其他类\活动,您可以拨打:
Text.setColor('some EditText you initialized');
,如果你有一个背景下,我的意思是里面;
public void onReceive(Context context, Intent intent){
}
您可以使用此代码来获得资源:
context.getResources().getString(R.string.app_name);
的辛格尔顿:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
初始化辛格尔顿在Application
子类:
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
如果我没有错,这会给你一个hook到applicationContext的地方,用ApplicationContextSingleton.getInstance.getApplicationContext();
来调用它。你不需要在任何时候清除它,因为当应用程序关闭的时候,它总是与它一起使用。
记住更新AndroidManifest.xml
使用此Application
子类:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
现在,你应该能够使用ApplicationContextSingleton.getInstance()getApplicationContext()从任何地方getResources(),还有极少数的地方。应用程序子类不能。
如果您在这里看到任何错误,请让我知道,谢谢。 :)
另一种解决方案:
如果在非静态外部类的静态子类,你可以从子类中通过在外部类的静态变量,就是你在创作的初始化访问的资源外部类。像
public class Outerclass {
static String resource1
public onCreate() {
resource1 = getString(R.string.text);
}
public static class Innerclass {
public StringGetter (int num) {
return resource1;
}
}
}
我用了我的FragmentActivity内的静FragmentPagerAdapter因为I8N的,这是有用的getPageTitle(INT位置)功能。
还有另一种可能性。我从加载资源的OpenGL着色器是这样的:
static private String vertexShaderCode;
static private String fragmentShaderCode;
static {
vertexShaderCode = readResourceAsString("/res/raw/vertex_shader.glsl");
fragmentShaderCode = readResourceAsString("/res/raw/fragment_shader.glsl");
}
private static String readResourceAsString(String path) {
Exception innerException;
Class<? extends FloorPlanRenderer> aClass = FloorPlanRenderer.class;
InputStream inputStream = aClass.getResourceAsStream(path);
byte[] bytes;
try {
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
innerException = e;
}
throw new RuntimeException("Cannot load shader code from resources.", innerException);
}
正如你所看到的,你可以在路径/res/...
变化aClass
访问任何资源类。这也是我如何加载测试资源(androidtest)
- 1. 从静态环境中获取资源
- 2. 如何获取网络资源内容?
- 3. 如何从静态上下文中获取getclass()。getResource()?
- 4. 获取静态资源的Spring MVC
- 5. 从我的jar文件中获取资源文件夹内容?
- 6. 如何从资源中获取文件
- 7. 不能从静态资源
- 8. 如何从android资源文件夹中读取html内容
- 9. 静态资源文件?
- 10. Spring Web:使用资源从Web上下文获取文件?
- 11. 如何刮取axd资源的内容?
- 12. 如何从JUnit测试中访问webapp下的静态资源?
- 13. 从静态上下文
- 14. Angular2 CLI问题与静态资源和上下文
- 15. 使用静态资源配置码头的上下文路径
- 16. 如何从静态上下文中引用非静态方法
- 17. 获取文件资源的静态方法
- 18. 如何在静态方法中获取此上下文
- 19. 获取非静态方法不能从静态上下文引用的错误
- 20. 与静态资源
- 21. 上下文webapp下面的灰熊静态内容路径
- 22. 资源下弹簧损坏pdf /静态
- 23. 获取从静态上下文参数Axis2中
- 24. 如何将所有静态内容保留在内容之下?
- 25. 非静态方法不能从**静态上下文**中引用。什么是静态内容?
- 26. Spring MVC - 从应用程序上下文外部访问静态资源
- 27. Spring Boot泽西静态内容与资源在“/”
- 28. 数据绑定按钮内容到静态资源失败
- 29. 设置静态资源的内容类型
- 30. 如何从BPL内提取资源?
整洁的伎俩,谢谢。好吧,那么没有标准的API呢? – 2010-12-09 18:03:09
这是标准的,这不是一个伎俩XD – Cristian 2010-12-09 18:13:05
我同意这是一个丑陋的黑客。永远不要为动态值赋予动态值 – Bostone 2012-02-22 23:46:39