【问题标题】:How to give Bungee font style to textview in android?如何将 Bungee 字体样式赋予 android 中的 textview?
【发布时间】:2017-04-15 17:15:16
【问题描述】:

我想在我的 android xml 代码中使用 Bungee 内联字体样式

  <RelativeLayout 
        android:id="@+id/sound_setting_layout"
        android:layout_width="500dip"
        android:layout_height="350dip"
        android:layout_marginTop="65dip"
        android:layout_marginLeft="780dip"
        android:layout_alignParentTop="true"
        android:padding="10dip"
        android:gravity="center"
        android:visibility="gone"
        android:background="@drawable/volume_layout"
        > 
<TextView
    android:layout_width="450dip"
    android:layout_height="50dip" 
    android:gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:text="Volume Control"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:textSize="30dip"        
    />

我尝试了很多,但我在 android 中找不到字体样式 Bungee。

【问题讨论】:

    标签: android xml layout fonts textview


    【解决方案1】:

    我们在 android 中没有默认的 bungee 字体样式,所以如果您想使用它,请下载 bungee 字体 .ttf 文件并在 assets 中创建一个名为 fonts 的文件夹并粘贴你下载的字体(.ttf)在那里 在这里可以下载Bungee字体:https://djr.com/bungee/ 在您的代码中执行此操作

     // Font path insted of bungee.ttf replace your .ttf file
        String fontPath = "fonts/bungee.ttf";
    
        // text view label which you want to apply Bungee font
        TextView txtGhost = (TextView) findViewById(R.id.androidSample);
    
        // here loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    
        // Applying font
        txtGhost.setTypeface(tf);
    

    【讨论】:

      【解决方案2】:

      将字体文件加载到 assets 文件夹 然后在您的活动 onCreate 中,使用以下方法

      Typeface face = Typeface.createFromAsset(YOUR_ACTIVITY.this.getAssets(),"fonts/YOUR_FONT_FILE_NAME.otf");
      your_text_view.setTypeface(face);
      

      【讨论】:

      • 如果它适合你,请尝试接受@Mhanddroid 的答案
      • 从哪里可以得到蹦极的字体文件
      • 你在哪里可以得到字体文件取决于你,搜索谷歌,例如下载蹦极字体等。字体可以有付费和免费版本,所以根据你的需要搜索
      【解决方案3】:

      如果您要在整个应用程序中使用自定义字体,例如在多个 TextView 上,最好使用单例模式,因为一遍又一遍地重新实例化字体会减慢您的应用程序的速度。

      试试这个类并用你自己的自定义字体替换字体路径,确保你的自定义字体在“main”中的“assets”文件夹中

      public class ProximaTypeface {
          public static ProximaTypeface instance = new ProximaTypeface();
      
          public ProximaTypeface() {
          }
      
          public Typeface regularTypeFace = null;
          public Typeface semiBoldTypeFace = null;
      
          public static ProximaTypeface getInstance() {
              return instance;
          }
      
          public void getRegularTypeface(Context context, TextView textView) {
              if (regularTypeFace == null) {
                  regularTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova_regular.otf");
              }
              textView.setTypeface(regularTypeFace);
          }
      
          public void getSemiBoldTypeface(Context context, TextView textView) {
              if (semiBoldTypeFace == null) {
                  semiBoldTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova.otf");
              }
              textView.setTypeface(semiBoldTypeFace);
          }
      
      }
      

      然后在你的活动中:

         ProximaTypeface proximaTypeface = new ProximaTypeface();
      
         TextView myTextView = (TextView) findViewById(R.id.textView);
      
         proximaTypeface.getRegularTypeface(context,myTextView);
      

      【讨论】:

      • 此解决方案在下面的 android 5.1 中是否正常运行???因为它使用反射
      • 我已经在针对 minSdkVersion 17(Android 4.2 Jelly Bean)的应用中对其进行了测试
      • 好的,我们能不能用它来设置应用程序本身的字体@Nour
      • 您必须对此进行一些研究并选择最适合您的解决方案,但您可以从这个答案开始stackoverflow.com/questions/2711858/…
      • 你提到的链接,5.1以下的解决方案失败
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2013-02-13
      • 2011-09-15
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多