自定义ActionBar完全覆盖Title

常规重写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);
		}
	}

actionbarlayout.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="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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值