【问题标题】:How to change the font of a main bar?如何更改主栏的字体?
【发布时间】:2014-02-07 02:05:24
【问题描述】:

我正在学习制作滑动菜单(抽屉)

我尝试过使用群里其他朋友的各种解决方案,但都没有奏效。

我无法更改主栏的字体。我会把 Agency_fb.ttf 源(已经把文件放在 assets 文件夹中)有朋友可以帮我解决这个问题吗?

我的编码是基于这个教程:点击here!!

http://i.imgur.com/eRgpn0n.png

感谢支持

【问题讨论】:

    标签: android fonts navigation-drawer


    【解决方案1】:

    我以前用过这个

    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayShowTitleEnabled(false);
    LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.titleview, null);
    ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
    this.getActionBar().setCustomView(v);
    
    
    //here is xml code
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >
    
    <com.your.package.CustomTextView
        android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:textSize="20dp"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      首先,将您的字体添加到项目的 assets 文件夹中 - 我通常将它们放在 assets/fonts 子文件夹中。在此之后,您只需使用自定义View 设置您的ActionBar,您在TextView 上设置了Typeface。例如:

      private void setUpActionBar(){
          final ActionBar bar = getActionBar();
      
          bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
      
          TextView mTitle = new TextView(this);
          mTitle.setText("SomeTitle");
          mTitle.setTextColor(getResources().getColor(android.R.color.holo_green_light));
          mTitle.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/YOURFONT.ttf"));
      
          ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      
          bar.setCustomView(mTitle, lp);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 2018-02-13
        • 1970-01-01
        • 2014-10-12
        • 2018-07-26
        相关资源
        最近更新 更多