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

2023-11-21

您好,我想为我的主要活动应用自定义背景布局。我找不到解决办法。有人可以给我一些建议吗?

希望在操作栏中有一个简单的带有背景的 TextView。这可能吗?

我设法删除图标并设置背景图像。但是如何将文本的重力设置为居中并更改字体呢?

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

It should look like this: enter image description here

thanks!


您必须为操作栏设置自定义布局,如下所示:

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

然后,您可以在布局中添加 TextView 并将其重力设置为center. E.g.:

<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>

然后,您可以从资产中的 Activity 中设置自定义字体,如下所示:

 TextView txt = (TextView) findViewById(R.id.mytext);  
 Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");  
 txt.setTypeface(font);  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ActionBarSherlock 中将标题重力设置为中心 的相关文章

随机推荐