中心对齐ActionBar标题&也使用自定义字体

时间:2012-11-07 00:53:56

标签: android customization android-actionbar

绝对的Android开发初学者,所以请耐心等待。我想在ActionBar中用我的标题实现两件事:

  1. 居中对齐标题文字
  2. 使用标题文字的自定义字体
  3. 我正在尝试按this回答,所以请先查看答案。

    MainActivity.java

    的内容
        // ATTEMPT TO STYLE THE ACTIONBAR
        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);
    
        //if you need to customize anything else about the text, do it here.
        //I'm using a custom TextView with a custom font in my layout xml so all I 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文件: layout / titleview.xml ,以对应上面的R.layout.titleview。它包含:

    <com.muppethead.app.name.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="MY APP TITLE" />
    

    这给出了错误: The following classes could not be found: com.muppethead.app.name.CustomTextView

    问题

    我哪里错了?除了修复上述错误之外,我在哪里引用我的字体,该字体位于assets/fonts/font.otf?那么以文本为中心呢?

    我要说,谷歌因为没有通过XML实现这一点而感到羞耻。它消除了新开发人员创造任何吸引力的可能性。

    提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我真诚地做的是忘记自定义TextView,只使用XML中的默认值。你只使用它一次,而且只用于自定义字体。

你可以这样做:

TextView tv = (TextView) v.findViewById(R.id.title);
Typeface tf = Typeface.createFromFile(getAssets(), "fonts/font.otf");
tv.setTypeface(tf);
tv.setText(this.getTitle());
相关问题