如何使android动作栏标题中心对齐?

我尝试了自己的“自定义操作”栏,以获取间的动作栏标题.有效.但是,在“操作”栏中添加选项菜单后,标题再次向左移动.为了让标题以动作栏的中间为中心,该怎么办?
我的代码是:
ActionBar actionBar = getActionBar();
    actionBar.setdisplayHomeAsUpEnabled(false);
    actionBar.setdisplayShowCustomEnabled(true);
    actionBar.setdisplayShowTitleEnabled(false);    
    actionBar.setdisplayUselogoEnabled(false);
    actionBar.setdisplayShowHomeEnabled(false);
    actionBar.setdisplayOptions(ActionBar.disPLAY_SHOW_CUSTOM); 
    View cView = getLayoutInflater().inflate(R.layout.custom_actionbar,null);
    actionBar.setCustomView(cView);

我的布局是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" >

<TextView
    android:id="@+id/apptitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:text="@string/app_name" />

解决方法

我相信,您正在使用以下设置您的自定义主题.
getSupportActionBar().setdisplayOptions(ActionBar.disPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);

如果没有,请添加第一行.并且您的自定义布局中的文本居中. (重力=中心)

另外我发现一个类似的链接herehere.希望这有帮助.

也试试这个.

我能够通过不显示标题来完成我想要的
该活动,而是使用您建议的customView.钥匙
我错过了一段时间,你必须使用setCustomView(View,
ActionVar.LayoutParams)方法,以获取自定义视图
居中,只需在自定义视图中设置一个layout_gravity =“center”即可
布局文件不起作用这是一个简短的代码段,显示了我如何
让它工作:

TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,null);
            ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.MATCH_PARENT,Gravity.CENTER);

            customView.setText("Some centered text");
            getSupportActionBar().setCustomView(customView,params);

这种方法在我至少在3.2,2.3和2.2设备上工作.

来源https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...