2013-03-29 177 views
3

我想弄清楚如何將操作欄選項卡上的字體更改爲某些自定義字體,並且對於我的生活我無法弄清楚。 IT似乎沒有辦法訪問Tab對象的底層TextView。更改ActionBar選項卡上的字體

課程的其他的解決方案,是定義自己的自定義動作條標籤佈局,並將其設置爲ActionBar.Tab對象上的自定義視圖,但這似乎相當棘手的還在努力。

如果我使用下面的XML佈局文件作爲我的自定義選項卡上的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text_tab" 
     style="@style/Widget.Sherlock.ActionBar.TabText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical"/> 

</LinearLayout> 

的動作條標籤似乎被忽略了我所有的重力的請求,並在標籤上定位文本視圖 - 這與默認情況下垂直居中顯示文本的標準操作欄選項卡完全不同。

如果有人可以建議a)爲操作欄選項卡提供自定義字體的更簡單的方法,或b)自定義選項卡視圖的正確佈局,我將非常感激。

謝謝。

+0

嘗試此... http://stackoverflow.com/questions/12665186/change-font-style-in-action-bar-tabs-using-sherlock –

+0

它仍然離開ActionBar.Tab文本頂部對齊,這不是我想要的。標準操作欄選項卡文本垂直居中。 – stork

回答

0

使用相對佈局。線性佈局別讓使用 機器人:layout_gravity =「center_vertical」 ......這僅允許使用中心水平..

如果使用相對佈局,你可以使用

android:layout_centerInParent="true" 
+0

不幸的是,這也不起作用,我試着將'RelativeLayout'設置爲'match_parent'並將'layout_centerInParent'設置爲true,但它沒有任何效果。 – stork

0

我想你應該設置你的layout_width和layout_height爲「FILL_PARENT」不WRAP_CONTENT 例如:

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

,使您的文本可以居中。

3

我不認爲這是可能的不幸。原因如下: 與您一樣,我嘗試使用以RelativeLayout爲中心的TextView作爲使用ActionBar.Tab.setCustomView()方法的我的選項卡的自定義視圖。我在RelativeLayout上使用了android:layout_width="fill_parent"android:layout_height="fill_parent",所以通常TextView應該居中。於是我試着只用TextView不是包裹在RelativeLayout像這樣:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab_title" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:gravity="center" 
    android:text="TAB_TITLE" /> 

所以正常情況下應該TextView完全填充標籤和文本應在其內部居中。然而,情況並非如此:文本仍然不是垂直居中。所以我決定把android:background="#ffffff"加到TextView,所以我可以看到發生了什麼。感謝背景,我意識到ActionBar.Tab.setCustomView()方法不會爲該選項卡設置自定義佈局,而是在另一個我們無權訪問的佈局中設置自定義視圖(AFAIK)。 android:layout_width="fill_parent"android:layout_height="fill_parent"在我的TextView中被忽略,因爲我使用null作爲膨脹標籤佈局時的父級。因此,爲了在TextView中居中文本,我以編程方式將TextView的高度設置爲非常高的值,以便填充其父項。然後文本垂直居中。請注意,您也可以使用頂部填充來創建相同的效果。這是我的代碼:

custom_tab。XML:在我的活動

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab_title" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:gravity="center" 
    android:text="TAB_TITLE" /> 

代碼來設置自定義視圖:

for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
    Tab tab = actionBar.newTab(); 
    TextView customTabView = (TextView)getLayoutInflater().inflate(R.layout.custom_tab, null); 
    customTabView.setText(mSectionsPagerAdapter.getPageTitle(i)); 
    // set the height to a really high value to fill the parent 
    customTabView.setHeight(999); 
    tab.setCustomView(customTabView); 
    tab.setTabListener(this); 
    actionBar.addTab(tab); 
} 

如果有人知道如何訪問組自定義視圖的父視圖,讓我知道。希望這可以幫助。

0

首先來看這個例子:簡單地在的onCreate

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/action_custom_title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="My Custom title" 
android:textColor="#fff" 
android:textSize="18sp" 
android:paddingTop="5dp" /> 

然後()Androidhive項目的MainActivity的方法,: [http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/][1]

然後創建一個佈局和用下面的代碼命名TAB_TITLE只是改變:

// Adding Tabs 
    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 

要:

// Adding Tabs 
    for (String tab_name : tabs) { 

     Tab tab = actionBar.newTab(); 
     TextView customTabView = (TextView)getLayoutInflater().inflate(R.layout.tab_title, null); 
     customTabView.setText(tab_name); 
     Typeface typface2=Typeface.createFromAsset(getAssets(),"fonts/titr.TTF"); 
     customTabView.setTypeface(typface2);    
     tab.setTabListener(this);   
     tab.setCustomView(customTabView); 
     actionBar.addTab(tab); 
    } 

(無需說您必須將字體/ titr.TTF更改爲您的資產目錄和文件名) 聯合它!

相關問題