2012-10-15 50 views
37

每個Android 4.0應用程序上都有一個ActionBar,它有一個標題。我需要減小這個尺寸。但我不明白我該怎麼做,因爲ActionBar不提供公共方法。 備註:我不想使用自定義視圖。如何更改操作欄上標題文本的大小?

+0

尼斯,乾淨的解決方案在這裏:http://stackoverflow.com/a/29266326/1617737 –

回答

3

Android ActionBar中的文字大小爲標準

由於ICS,Android現在開發了一個標準,以便應用程序(希望)遵循相同的UI準則和設計原則。

http://developer.android.com/guide/practices/ui_guidelines/index.html

這就是爲什麼你不能改變它。

然而,如果你還是想這樣做,你可以使用自定義主題或可能實現動作條(叉),並修改其源:https://github.com/johannilsson/android-actionbar得到最大程度的控制。

+3

你可以。但你不應該。 Android的chrome由UI專業人員設計,他們研究用戶如何與設備進行交互,並優化所有UI元素的外觀和佈局,以促進良好的交互。修改Chrome會使應用程序的可用性降低並使用戶受挫。 – 323go

47

其實,你可以做你想要自定義的ActionBar。它必須通過主題完成。你可以做這樣的事情:

<style name="Theme.YourTheme" parent="android:style/Theme"> 
    <item name="android:actionBarStyle">@style/Theme.YourTheme.Styled.ActionBar</item> 
</style> 

<style name="Theme.YourTheme.Styled.ActionBar"> 
    <item name="android:titleTextStyle">@style/Theme.Viadeo.Styled.YourTheme.TitleTextStyle</item> 
</style> 

<style name="Theme.YourTheme.Styled.ActionBar.TitleTextStyle" parent="@android:style/Widget.TextView"> 
    <item name="android:textSize">12sp</item> 
</style> 
+2

謝謝Aurélien!你應該考慮使用dp的sp inhead作爲文本大小;-) – gingo

+2

你是對的:) –

+8

而不是硬編碼任何大小,你可以考慮從'parent =「@ android:style/TextAppearance.Large」'繼承。無論您使用什麼設備,這都會爲您提供更大的文本大小。 –

18

你總是可以做這樣的事情:

ActionBar actionBar = getActionBar(); 
actionBar.setTitle(Html.fromHtml("<small>YOUR TITLE</small>")); 
+0

這不工作,你將不得不添加文本視圖 – Amrut

+0

工作對我來說,使用Sherlock –

+0

多數民衆贊成在良好的工作,它爲我工作 – Milaaaad

7

在我的系統,RES /風格AppTheme有一個紙條,上面寫着

<!-- All customizations that are NOT specific to a particular API-level can go here. --> 

這是AndroidMainfest.xml中指示的樣式。我加

<item name="android:textSize">15sp</item> 

它的工作,我寧願這樣做比自定義操作欄。

1

我按照這個方法來跳過自定義樣式創建:

  1. 創建所需的文本大小和
  2. 自定義佈局應用,爲你的動作條。

1)創建一個佈局文件(titlebar.xml):

<?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="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/action_bar_title" 
     android:text="@string/app_name" 
     android:textColor="@color/colorPrimary" 
     android:textSize="24sp" /> 
</LinearLayout> 

這裏設置您需要TEXTSIZE。

2)MainActivity.java

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //bellow setSupportActionBar(toolbar); 
    getSupportActionBar().setCustomView(R.layout.titlebar); 

希望這會有所幫助。