Android 更改操作栏选项卡上的字体

Android 更改操作栏选项卡上的字体,android,android-fragments,Android,Android Fragments,我正试图弄清楚如何将actionbar选项卡上的字体更改为某种自定义字体,但我一辈子都搞不清楚。似乎没有任何方法可以访问选项卡对象的底层TextView 当然,另一个解决方案是定义我自己的自定义ActionBar选项卡布局,并将其设置为ActionBar.tab对象上的自定义视图,但这似乎也很难实现 如果使用以下XML布局文件作为自定义选项卡布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi

我正试图弄清楚如何将actionbar选项卡上的字体更改为某种自定义字体,但我一辈子都搞不清楚。似乎没有任何方法可以访问选项卡对象的底层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>

actionbar选项卡似乎忽略了我所有的重力请求,并将文本定位在选项卡视图的顶部——这与默认情况下文本垂直居中的标准actionbar选项卡大不相同

如果有人能建议a)为actionbar选项卡提供自定义字体的更简单方法,或b)为自定义选项卡视图提供正确布局,我将非常感激


谢谢。

使用相对布局。线性布局不允许使用 android:layout\u gravity=“center\u vertical”。。。。它只允许使用“中心水平”

如果使用相对布局,则可以使用

android:layout_centerInParent="true"

我认为您应该将布局宽度和布局高度设置为“填充父对象”,而不是包装内容 例如:


这样你的文本就可以居中了。

不幸的是,我认为这是不可能的。原因如下: 和您一样,我尝试使用
ActionBar.Tab.setCustomView()
方法,将
TextView
集中在
RelativeLayout
中,作为选项卡的自定义视图。我在
RelativeLayout
上使用了
android:layout\u width=“fill\u parent”
android:layout\u height=“fill\u parent”
,因此通常
文本视图应该居中。因此,我尝试只使用
文本视图
而不使用
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" />
“我的活动”中设置自定义视图的代码:

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);
}
for(int i=0;i

如果有人知道如何访问set自定义视图的父视图,请告诉我。希望这能有所帮助。

首先看看这个例子: [

然后创建一个布局,并使用以下代码将其命名为tab_title:

<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" />
致:

(无需说明您必须将font/titr.TTF更改为资产目录和文件名)
连接它!!

试试这个…它仍然保持操作栏。选项卡文本顶部对齐,这不是我想要的。标准的操作栏选项卡文本垂直居中。不幸的是,这也不起作用,我尝试将
RelativeLayout
设置为
match\u parent
并将
layout\u centerInParent
设置为true,但没有任何影响。
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);
}
<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" />
 // 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);
    }