使用setCustomView建立标题居中的自定义ActionBar

ActionBar因为溢出菜单的原因导致没有办法让标题居中,但是我们又经常有样式需要标题居中,所以使用自定义ActionBar的布局是最正确的选择,可以在xml布局文件中进行视图的定制。

定制使用到的api是ActionBar中提供的 void setCustomView(View) 函数。

定义的视图是这个样子的:

使用setCustomView建立标题居中的自定义ActionBar


标题终于居中了。

这里没有涉及到任何ActionBar的style修改,也只有一个activity;

MainActivity.java

  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         // 返回箭头(默认不显示)  
  8.          getActionBar().setDisplayHomeAsUpEnabled(false);  
  9.         // 左侧图标点击事件使能  
  10.         getActionBar().setHomeButtonEnabled(true);  
  11.         // 使左上角图标(系统)是否显示  
  12.         getActionBar().setDisplayShowHomeEnabled(false);  
  13.         // 显示标题  
  14.         getActionBar().setDisplayShowTitleEnabled(false);  
  15.         //显示自定义视图  
  16.         getActionBar().setDisplayShowCustomEnabled(true);  
  17.         View actionbarLayout = LayoutInflater.from(this).inflate(  
  18.                 R.layout.actionbar_layout, null);  
  19.         getActionBar().setCustomView(actionbarLayout);  
  20.     }  
  21. }  

actionbar_layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center_vertical"  
  6.     android:paddingLeft="10dp"  
  7.     android:paddingRight="10dp" >  
  8.   
  9.   
  10.     <ImageButton  
  11.         android:id="@+id/left_imbt"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_centerVertical="true"  
  16.         android:background="@null"  
  17.         android:src="@drawable/b" />  
  18.   
  19.     <TextView  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_centerInParent="true"  
  23.         android:textColor="#FFFFFFFF"  
  24.         android:textSize="18sp"  
  25.         android:text="标题" />  
  26.     <!-- actionbar 右边按钮 -->  
  27.   
  28.     <ImageButton  
  29.         android:id="@+id/right_imbt"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_alignParentRight="true"  
  33.         android:layout_centerVertical="true"  
  34.         android:background="@null"  
  35.         android:src="@drawable/k" />  
  36.   
  37. </RelativeLayout>  
补充两个用到的图标,透明白色图标(白色图标看不到,在空白处点击另存为就好)

b.png:使用setCustomView建立标题居中的自定义ActionBar;k.png使用setCustomView建立标题居中的自定义ActionBar


虽然只有这两个文件用到和修改到,但是最小的版本也要支持ActionBar;

这里补充一下setHomeButtonEnabled函数,控制左上角图标是否显示出来,小于4.0版本的默认值为true的。在4.0及其以上默认值是false;

本文来自****博客 转载请联系作者
并注明出处http://blog.****.net/dreamintheworld/article/details/39314121