简体   繁体   English

将标题重力设置为ActionBarSherlock中心

[英]Set Title gravity to center in ActionBarSherlock

Hello I would like to apply a custom Backgroundlayout just for my Main Activity. 您好我想为我的主要活动应用自定义Backgroundlayout。 I couldn't find a solution. 我找不到解决方案。 Can somebody give me some tips? 有人可以给我一些提示吗?

would like to have just a simple TextView with a background in the Actionbar. 想在Actionbar中只有一个带背景的简单TextView。 Is this possible? 这可能吗?

I managed to remove the icon and set a backgroundimage. 我设法删除图标并设置backgroundimage。 But how can I set the gravity of the text to center and change the typefont? 但是,如何将文本的重力设置为居中并更改typefont?

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

It should look like this: 它应该如下所示: 在此输入图像描述

thanks! 谢谢!

You have to set a custom layout for the Actionbar like this: 您必须为Actionbar设置自定义布局,如下所示:

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

In your layout you can then add a TextView and set its gravity to center . 在您的布局中,您可以添加TextView并将其重力设置为center Eg: 例如:

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

Then you can set your custom typefont inside your Activity from your assets like this: 然后,您可以在资源中设置自定义typefont,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM