除MainActivity.class以外的其他类中无法识别getAssets()

时间:2018-10-07 21:24:58

标签: android

我无法通过Typeface放置自定义字体,因为在编写createFromAsset时未显示getAssets()。 我使用过getContext(),getActivity(),将资产放置在项目中而不是src中,但都找不到soln。 请告诉我错误。

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

public class DisplayText {
   /** 
    * Displays text to window
    * 
    * PARAMETERS:
    * g2d            A Graphics2D object
    * text           The text to be drawn to screen
    * fontName       The name of the font to be used.
    * fontType       Whether the font is bold, italic, or neither.
    * color          Array of length 3, to represent RGB color
    * xPos           The x-position of the text.    
    * yPos           The y-position of the text.
    * fontSize       The size of the font.
    * menuSpacing    How far apart menu text should be. (Never zero!)
    */
   public static void displayText(Graphics2D g2d,
         String[] textItems, String fontName, String fontType, 
         int[] color,
         int xPos, int yPos, int fontSize, double menuSpacing) {

      // Check for problems
      // if textItems is empty
      if (textItems.length < 1) {
         System.out.println("DisplayText.displayText() ERROR" +
               System.lineSeparator() +
               "no items in String[] textItems");
         return;
      // if color array isn't length 3.
      } else if (color.length != 3) {
         System.out.println("DisplayText.displayText() ERROR" +
               System.lineSeparator() +
               "color.length != 3");
         return;
      // if menuSpacing isn't a positive number or > 0.
      } else if (menuSpacing <= 0) {
         System.out.println("DisplayText.displayText() ERROR" +
               System.lineSeparator() +
               "menuSpacing <= 0");
      }

      // assign appropriate int according to fontType
      fontType = fontType.toLowerCase();
      int fontNum;
      switch(fontType) {
         case "bold" :
            fontNum = Font.BOLD;
            break;
         case "italic" :
            fontNum = Font.ITALIC;
            break;
         case "plain" :
            fontNum = Font.PLAIN;
            break;
         default :
            fontNum = Font.PLAIN;
            break;
      }

      g2d.setFont(new Font(fontName, fontNum, fontSize));
      g2d.setColor(new Color(color[0], color[1], color[2]));

      // Determine whether to print several lines or one.
      if (textItems.length == 1) {
         g2d.drawString(textItems[0], xPos, yPos);
      } else { 
         // Draw several lines 
         for (int i = 0; i < textItems.length; i++) {
            g2d.drawString(textItems[i], xPos, yPos);
            // Spaces menu items by the height of the font and menuSpacing
            yPos += (int) (g2d.getFontMetrics().getAscent() * menuSpacing);
         }
      }
   }

   // Finds the x-position centered text should be at.
   public static int findHorizontalCenter(Graphics2D g2d,
         String s, int w) {
     Font f = g2d.getFont();
     FontMetrics fm = g2d.getFontMetrics(f);
     int width = fm.stringWidth(s);
     System.out.println(width);
     int x = (MainClass.WINDOW_WIDTH  - width)/2;
     return x;
   }


}

2 个答案:

答案 0 :(得分:0)

您必须将资源放置在 <Location /MyApp-rest> SetHandler weblogic-handler WLLogFile /opt/MyApp/MyApp-rest.log Debug OFF WebLogicHost 127.0.0.1 WebLogicPort 23777 WLSocketTimeoutSecs 60 </Location> <Location /MyApp-gui> SetHandler weblogic-handler WLLogFile /opt/MyApp/MyApp-gui.log Debug OFF WebLogicHost 127.0.0.1 WebLogicPort 23777 WLSocketTimeoutSecs 60 </Location> 文件夹下,然后放置Fonts

Antipasto-RegularTrial.ttf

答案 1 :(得分:0)

RecyclerView.ViewHolder范围内,您可以像下面的代码所示获得Context的句柄(为了使XML预览正常工作,它还考虑了View.isInEditMode()):< / p>

/* obtain a handle to the parent RecyclerView */
this.mRecyclerView = (SomeRecyclerView) viewHolder.getParent();

/* obtain a handle to the it's Context */
Context context;
if(viewHolder.isInEditMode()) {context = ((ContextThemeWrapper) this.mRecyclerView.getContext()).getBaseContext();}
else  {context = this.mRecyclerView.getContext();}

很有可能,Fonts/Antipasto-RegularTrial.ttf应该重命名为fonts/antipasto_regulartrial.ttf,以便成为有效资源描述符。资产和资源之间可能有所不同;参见Fonts in XML-这说明了如何同时进行。