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

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

how to get context inside MainActivity.java

问题

以下是您要翻译的内容:

我正在尝试为React Native应用程序创建一个启动画面
我已经成功了但我必须为标题添加自定义字体

以下是我用于实现它的代码但显然它不起作用

package com.hackerkid;

import android.os.Bundle;
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import android.widget.TextView;
import android.graphics.Typeface;
import android.content.Context;

public class MainActivity extends ReactActivity {
  /**
   * 返回从JavaScript注册的主组件的名称。这用于安排组件的呈现。
   */
  @Override
  protected String getMainComponentName() {
    return "hackerkid";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(savedInstanceState);
    TextView splashText = (TextView) findViewById(R.id.splashTitle);

    Typeface face = Typeface.createFromAsset(this.getAssets(), "fonts/FredokaOne-Regular.ttf");
    splashText.setTypeface(face);
  }
}

`getAssests()` 不起作用我无法从中获取当前上下文的资产
我是Android开发的新手
我尝试过`getApplicationContext()`,但没有成功

请指导我如何在MainActivity.java中获取上下文

谢谢

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

英文:

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.

package com.hackerkid;

import android.os.Bundle;
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import android.widget.TextView;
import android.graphics.Typeface;
import 	android.content.Context;

public class MainActivity extends ReactActivity {
  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "hackerkid";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(savedInstanceState);
    TextView splashText = (TextView) findViewById(R.id.splashTitle);

    Typeface face = Typeface.createFromAsset(this.getAssets(), "fonts/FredokaOne-Regular.ttf");
    splashText.setTypeface(face);
  }
}

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类中使用这个上下文。根据您的代码,请查看以下代码片段:

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "hackerkid";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView splashText = findViewById(R.id.splashTitle);

    // 这个`getApplicationContext()`是从ReactActivity类中获取的。

    Typeface face = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/FredokaOne-Regular.ttf");
    splashText.setTypeface(face);
  }
}
英文:

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

Check the following code snippet as per your code:

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "hackerkid";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView splashText = findViewById(R.id.splashTitle);

    // This getApplicationContext() is from ReactActivity class.

    Typeface face = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/FredokaOne-Regular.ttf");
    splashText.setTypeface(face);
  }
}

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:

确定