在MainActivity.java中获取上下文的方法如何实现?

huangapple go评论113阅读模式
英文:

how to get context inside MainActivity.java

问题

以下是您要翻译的内容:

  1. 我正在尝试为React Native应用程序创建一个启动画面
  2. 我已经成功了但我必须为标题添加自定义字体
  3. 以下是我用于实现它的代码但显然它不起作用
  4. package com.hackerkid;
  5. import android.os.Bundle;
  6. import com.facebook.react.ReactActivity;
  7. import org.devio.rn.splashscreen.SplashScreen;
  8. import android.widget.TextView;
  9. import android.graphics.Typeface;
  10. import android.content.Context;
  11. public class MainActivity extends ReactActivity {
  12. /**
  13. * 返回从JavaScript注册的主组件的名称。这用于安排组件的呈现。
  14. */
  15. @Override
  16. protected String getMainComponentName() {
  17. return "hackerkid";
  18. }
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. SplashScreen.show(this);
  22. super.onCreate(savedInstanceState);
  23. TextView splashText = (TextView) findViewById(R.id.splashTitle);
  24. Typeface face = Typeface.createFromAsset(this.getAssets(), "fonts/FredokaOne-Regular.ttf");
  25. splashText.setTypeface(face);
  26. }
  27. }
  28. `getAssests()` 不起作用我无法从中获取当前上下文的资产
  29. 我是Android开发的新手
  30. 我尝试过`getApplicationContext()`但没有成功
  31. 请指导我如何在MainActivity.java中获取上下文
  32. 谢谢

请注意,这段代码中的注释已经翻译为中文,代码部分保持原样。

英文:

I am trying to do a splash screen for a React Native application.
I have succeeded but I have to add a custom font to the heading.

Below is my code for doing it, but clearly it is not working.

  1. package com.hackerkid;
  2. import android.os.Bundle;
  3. import com.facebook.react.ReactActivity;
  4. import org.devio.rn.splashscreen.SplashScreen;
  5. import android.widget.TextView;
  6. import android.graphics.Typeface;
  7. import android.content.Context;
  8. public class MainActivity extends ReactActivity {
  9. /**
  10. * Returns the name of the main component registered from JavaScript. This is used to schedule
  11. * rendering of the component.
  12. */
  13. @Override
  14. protected String getMainComponentName() {
  15. return "hackerkid";
  16. }
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. SplashScreen.show(this);
  20. super.onCreate(savedInstanceState);
  21. TextView splashText = (TextView) findViewById(R.id.splashTitle);
  22. Typeface face = Typeface.createFromAsset(this.getAssets(), "fonts/FredokaOne-Regular.ttf");
  23. splashText.setTypeface(face);
  24. }
  25. }

getAssests() is not working, I am not able to get the current context to getAssests from it.
I am very new to android development.
I have tried getApplicationContext() but it did not work.

Kindly guide me how to get the context inside MainActivity.java

Thanks

答案1

得分: 0

ReactActivity类提供了getApplicationContext(),因此在MainActivity.java类中使用这个上下文。根据您的代码,请查看以下代码片段:

  1. public class MainActivity extends ReactActivity {
  2. @Override
  3. protected String getMainComponentName() {
  4. return "hackerkid";
  5. }
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. SplashScreen.show(this);
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. TextView splashText = findViewById(R.id.splashTitle);
  12. // 这个`getApplicationContext()`是从ReactActivity类中获取的。
  13. Typeface face = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/FredokaOne-Regular.ttf");
  14. splashText.setTypeface(face);
  15. }
  16. }
英文:

ReactActivity class class provides you getApplicationContext(), so use this context inside MainActivity.java class.

Check the following code snippet as per your code:

  1. public class MainActivity extends ReactActivity {
  2. @Override
  3. protected String getMainComponentName() {
  4. return "hackerkid";
  5. }
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. SplashScreen.show(this);
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. TextView splashText = findViewById(R.id.splashTitle);
  12. // This getApplicationContext() is from ReactActivity class.
  13. Typeface face = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/FredokaOne-Regular.ttf");
  14. splashText.setTypeface(face);
  15. }
  16. }

huangapple
  • 本文由 发表于 2023年3月9日 14:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定