【问题标题】:Set Title gravity to center in ActionBarSherlock在 ActionBarSherlock 中将标题重力设置为中心
【发布时间】:2012-08-25 19:47:49
【问题描述】:

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

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

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

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

它应该是这样的:

谢谢!

【问题讨论】:

    标签: android android-actionbar actionbarsherlock android-theme


    【解决方案1】:

    您必须像这样为操作栏设置自定义布局:

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

    然后您可以在您的布局中添加一个 TextView 并将其重力设置为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>
    

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

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

    【讨论】:

    • 如果您有任何选项卡,这将是非常错误的。选项卡仍然存在,但标题位于 Htem 下方
    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2011-12-21
    • 2012-01-25
    • 1970-01-01
    • 2012-04-09
    相关资源
    最近更新 更多