2014-02-14 27 views
9

我尝试了我自己的自定义操作栏,以获取以中间为中心的操作栏标题。有效。但是,在操作栏中添加了选项菜单后,标题再次向左移动。我应该怎么做才能让标题集中在操作栏的中间位置? 我的代码是:如何让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" /> 

+0

你可以发布你的代码,你试图集中标题? –

+0

http://stackoverflow.com/a/23841756/896322这里也许你可以找到解决方案 –

回答

15

我相信,您使用下面的设置你的自定义主题。

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

如果不是,请添加第一行。并且您的自定义布局中的文本居中。 (Gravity = Center)

我还发现了类似的链接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

+0

我试过了。但它不工作。 – Anu

+0

您是否也检查链接中的解决方案?并且,在你的RelativeLayout中尝试设置android:layout_width =“match_parent”。 –

+0

这可以工作,但会删除导航菜单的按钮。在Android的ActionBar血腥的恶臭,所以我一直怀疑,我最终不得不推出自己的... –

2

首先,创建一个.xml文件是这样的:(action_bar.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<TextView 
    android:id="@+id/actionbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:maxLines="1" 
    android:clickable="false" 
    android:focusable="false" 
    android:longClickable="false" 
    android:textStyle="bold" 
    android:textSize="18sp" 
    android:textColor="#000000" /> 

</LinearLayout> 

,然后在你的活动:

View view = getLayoutInflater().inflate(R.layout.action_bar, null); 
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
      ActionBar.LayoutParams.WRAP_CONTENT, 
      ActionBar.LayoutParams.MATCH_PARENT, 
      Gravity.CENTER); 

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title); 
    Title.setText("Your Title"); 

getSupportActionBar().setCustomView(view,params);  
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title 
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title 

我试着很多解决方案,只有这一个工程。希望它可以帮助你。