2014-02-22 64 views

回答

1

您应该为ActionBar进行自定义布局。

在Java:

getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getActionBar().setCustomView(R.layout.actionbar); 

在XML(这将是R.layout.actionbar):

<?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:layout_gravity="center" 
    android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="YOUR ACTIVITY TITLE" 
    android:textStyle="bold" 
    android:textColor="#ffffff" 
    android:textSize="24sp" /> 
</LinearLayout> 

您可以然后把更多的东西在动作条XML如果你想。

+0

谢谢你的回复,但是这里没有后退按钮 – javagc

+0

尝试用'getActionBar()。setDisplayHomeAsUpEnabled(true);' – ArianJM

+0

结果是一样的:( – javagc

3

您需要设置具有自定义布局的操作栏。 像这样根据您的需要

action = getActionBar(); 

     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL 
         | Gravity.CENTER_VERTICAL); 
     View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)) 
       .inflate(R.layout.customlayout, null); 

action.setCustomView(v, lp); 

     action.setDisplayShowCustomEnabled(true); 
     action.setDisplayHomeAsUpEnabled(true); 
     action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

修改代码:您需要创建自定义布局:R.layout.customlayout。