簡體   English   中英

在android中更改應用程序標題的字體

[英]Changing the font of the application title in android

我有一個字體,我想在android中更改動作欄標題的字體。 有沒有辦法像這樣設置標題字體?

this.setTitle(myTitle.toUpperCase()); 
this.setTypefaceofTitle(tf);

這不是一個復制問題,這個鏈接上的方法( 如何在ActionBar標題中設置自定義字體? )不起作用。 當我嘗試它們時,eclipse會給出錯誤: java.lang.noSuchMethodError

嘗試這個,

    int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
Typeface robotoBoldCondensedItalic = Typeface.createFromAsset(getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
if(actionBarTitleView != null){
    actionBarTitleView.setTypeface(robotoBoldCondensedItalic);
}

如果它的工具欄意味着嘗試如下。

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_color”
app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:layout_gravity="center"
    android:id="@+id/title_txt” />


</android.support.v7.widget.Toolbar>

然后使用以下注釋以編程方式添加任何樣式。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.title);

要么

改變使用風格。 需要一個信息點擊這里

我從headvk找到了這個幫助我的答案 做一些調整我只需要這樣做:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
TextView myTitle = (TextView) toolbar.getChildAt(0);
myTitle.setTypeface(font);

在我的應用程序上做一些測試我發現標題TextView是工具欄的第一個孩子,所以現在我擁有它我可以設置它的更改。

https://stackoverflow.com/a/8748802/1943671你可以在這個答案中做。

將自定義視圖設置為操作欄之后,就像在鏈接中一樣,只需為TextView指定字體:

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf");
TextView tv = (TextView) findViewById(R.id.customTitle);
tv.setTypeface(tf);

您可以通過創建自定義視圖並在ActionBar.膨脹該自定義視圖來實現ActionBar.
此自定義視圖將包含您將獲得引用的TextView ,然后可以設置字體。

請參閱此處的示例: 如何在ActionBar標題中設置自定義字體?

它不受支持。但您可以為操作欄創建自定義視圖。 如何在ActionBar標題中設置自定義字體?

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    //getMenuInflater().inflate(R.menu.activity_main, menu);
    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayShowTitleEnabled(false);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/architectsdaughter.ttf");


    LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.xml_master_footer, null);


    this.getActionBar().setCustomView(v);

    ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
    ((TextView) v.findViewById(R.id.title)).setTypeface(tf);


    //assign the view to the actionbar

    return true;
}

在你的清單中

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM