【问题标题】:Actionbar title font not changing in android动作栏标题字体在android中没有改变
【发布时间】:2015-07-02 15:21:34
【问题描述】:

我想将自定义字体更改为我的操作栏标题,已经通过了几个接受的答案,但没有运气,我尝试如下,但我的自定义字体不适用于我的操作栏标题,请帮助我摆脱这个,我的代码如下:

java

Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
                "neuropolx.ttf");
        SpannableString s = new SpannableString(
                "                     KOLAY RANDEVU");
        s.setSpan(new CustomTypefaceSpan("", font), 0, 4,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        getActivity().getActionBar().setTitle(s);

【问题讨论】:

    标签: android android-actionbar android-theme android-styles


    【解决方案1】:

    您可以使用此代码解决此问题

    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayShowTitleEnabled(false);
    

    【讨论】:

      【解决方案2】:

      不完全支持更改操作栏标题字体,但您可以为操作栏使用自定义视图。使用自定义视图并禁用原生标题。它将显示在图标和操作项之间。您可以从单个活动中继承所有活动,该活动在 onCreate 中有以下代码:

      this.getActionBar().setDisplayShowCustomEnabled(true);
      this.getActionBar().setDisplayShowTitleEnabled(false);
      
      LayoutInflater inflator = LayoutInflater.from(this);
      View v = inflator.inflate(R.layout.customTitleView, null);
      
      //you can customise anything about the text here.
      //Using a custom TextView with a custom font in layout xml so all you need to do is set title
      ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
      
      //assign the view to the actionbar
      this.getActionBar().setCustomView(v);
      

      你的布局 xml(上面代码中的 R.layout.customTitleView)看起来像这样:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 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="" />
      </LinearLayout>
      

      【讨论】:

      • 您好,谢谢先生的回复,但是现在我面临一个新问题,当我的标题出现在操作栏中两次时
      【解决方案3】:

      您可以制作自己的自定义操作栏,然后更改标题字体。您可以将字体文件放在资产文件夹中并从中访问。试试这样:

      tf= Typeface.createFromAsset(getAssets(), "FontType.TTF");
      
       actionBar.setDisplayShowCustomEnabled(true);
          actionBar.setDisplayShowTitleEnabled(false);
          actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#9B66AB")));
      
          LayoutInflater inflater = LayoutInflater.from(c);
          @SuppressLint("InflateParams")
          View v = inflater.inflate(R.layout.titleview_2, null);
      
          TextView actionbar_title = (TextView)v.findViewById(R.id.actionbar_title);
          actionbar_title.setTypeface(tf);
      
          actionBar.setCustomView(v);
      

      它的xml文件会是这样的:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:id="@+id/actionBar"
      android:paddingTop="7dp"
      android:orientation="horizontal"
      android:background="#9B66AB">
      
      <TextView
          android:id="@+id/actionbar_title"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal|center_vertical"
          android:gravity="center_horizontal|center_vertical"
          android:padding="8dp"
          android:text="@string/appTitle"
          android:textSize="20sp"
          android:textColor="#FFFFFF"
          android:layout_centerVertical="true" />
      </RelativeLayout>
      

      希望对你有帮助...

      【讨论】:

        【解决方案4】:

        使用

        getActivity().getActionBar().setCustomView(myTextView);
        

        在该视图中,您可以使用 TextView,您可以为其设置您选择的字体

        TextView myTextView = new TextView(context);        
              myTextView.setTypeface(font);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多