在ActionBarSherlock中将标题重心设置为居中

9

你好,我想为我的主要活动应用自定义背景布局。我找不到解决方案。有人能给我一些提示吗?

我只想在操作栏中有一个带背景的简单TextView。这是可能的吗?

我成功移除了图标并设置了背景图片。但是如何将文本重心设置为中心并更改字体类型呢?

getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(myimage)); 

应该长这样: 在此输入图像描述 谢谢!
1个回答

16

您需要像这样设置操作栏的自定义布局:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.yourlayout);

在你的布局中,你可以添加一个TextView并将它的gravity属性设置为center,例如:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Header!"
        android:id="@+id/mytext"
        android:textColor="#ffffff"
        android:textSize="25dp" />

</LinearLayout>

然后,您可以从资产中在活动中设置自定义字体类型,如下所示:

 TextView txt = (TextView) findViewById(R.id.mytext);  
 Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");  
 txt.setTypeface(font);  

3
如果您有任何选项卡,这将出现严重问题。选项卡仍然存在,但标题会显示在它们的下方。 - Adam

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