常规重写ActionBar的方法无法完全覆盖ActionBar,通过重载onCreateOptionsMenu函数可以做到完全覆盖的效果。
效果图:
实现方法:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); setActionBarLayout(R.layout.actionbarlayout, this); return true; }
/** * 设置ActionBar的布局 * * @param layoutId * 布局Id * * */ @SuppressLint("NewApi") public void setActionBarLayout(int layoutId, Context mContext) { ActionBar actionBar = getActionBar(); if (null != actionBar) { actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(layoutId, new LinearLayout(mContext), false); ActionBar.LayoutParams layout = new ActionBar.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); actionBar.setCustomView(v, layout); } }
<?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="match_parent" android:background="#FFFF8824" android:orientation="horizontal" > <Button android:id="@+id/btnActionBarBack" android:layout_width="0px" android:layout_height="fill_parent" android:layout_weight="2" android:text="test" /> <TextView android:id="@+id/tvActionBarName" android:layout_width="0px" android:layout_height="fill_parent" android:layout_weight="6" android:gravity="center" android:text="Hello" /> <Button android:id="@+id/btnActionBarSearch" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="2" android:text="test" /> </LinearLayout>
http://download.csdn.net/download/miaoyunzexiaobao/8715065
转载请注明出处:http://blog.csdn.net/miaoyunzexiaobao
原文地址:http://blog.csdn.net/miaoyunzexiaobao/article/details/45871475