更改ActionBar选项卡的字体

3
我正在试图弄清楚如何更改操作栏选项卡上的字体为一些自定义字体,但是我却想不出来。似乎没有办法访问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>

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

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

谢谢。


试试这个...https://dev59.com/bmcs5IYBdhLWcg3wp133 - Aaron McIver
它仍然使ActionBar.Tab文本顶部对齐,这不是我想要的。标准的ActionBar选项卡文本在垂直方向上居中。 - stork
4个回答

3
很遗憾,我不觉得这是可能的。原因如下: 像您一样,我尝试使用ActionBar.Tab.setCustomView()方法中一个在RelativeLayout中居中的TextView作为我的选项卡的自定义视图。 我在RelativeLayout上使用了android:layout_width="fill_parent"android:layout_height="fill_parent" ,所以通常情况下TextView应该居中显示。然后我尝试只使用没有包含在RelativeLayout中的TextView
<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应该完全填充选项卡,文本应在其中居中。但是仍然不是这种情况:文本仍然没有垂直居中。因此,我决定为TextView添加android:background="#ffffff",以便我可以看到发生了什么。通过背景,我意识到ActionBar.Tab.setCustomView()方法并没有为选项卡设置自定义布局,而是在我们无法访问的另一个布局中设置自定义视图(据我所知)。使用null作为父级填充选项卡布局时,android:layout_width="fill_parent"android:layout_height="fill_parent"被忽略,这就解释了为什么我的TextView中使用它们没有效果。因此,为了使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

首先看一下这个例子: [http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/][1]

然后创建一个布局并将其命名为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" />

然后在 Androidhive 项目的 MainActivity 的 onCreate() 方法中,只需更改以下内容:
 // 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更改为您的资产目录和文件名

联合起来吧!


0
我认为你应该将layout_width和layout_height设置为“fill_parent”,而不是“wrap_content”这样:
android:layout_width="fill_parent"
android:layout_height="fill_parent"

这样您的文本就可以居中显示。


0
使用相对布局。线性布局不允许使用android:layout_gravity="center_vertical"....只能使用水平居中。
如果您使用相对布局,就可以使用。
android:layout_centerInParent="true"

不幸的是,这也不起作用,我尝试将RelativeLayout设置为match_parent,并将 layout_centerInParent设置为true,但它没有任何效果。 - stork

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接