android actionbar标题栏

在android的actionBar中,actionBar的视图是固定的,左边是程序的图标和title,右边是添加的menuItem,如果想要定制actionbar中的view就要自定义视图。

首先要定义actionbar的xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout01"
    >
    
<ImageButton android:id="@+id/left"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
            android:background="@android:color/transparent"
           android:src="@drawable/show_left"
           android:layout_gravity="left|center_vertical"
           android:paddingLeft="8dip"
           android:clickable="true"/>   
           
<TextView android:id="@+id/tv"
          android:background="@android:color/transparent"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:text="首页"
          android:textColor="@android:color/white"
          android:textSize="18dip"
          android:paddingTop="2dip"
          android:layout_gravity="center"/>   
          
<ImageButton android:id="@+id/more"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:background="@android:color/transparent"
           android:src="@drawable/cross"
           android:paddingRight="8dip"
           android:clickable="true"
           android:layout_gravity="right|center_vertical"/>  
</FrameLayout>

然后在onCreateMenuItem设置actionBar的样式,一定要设 置 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)和 actionBar.setDisplayShowCustomEnabled(true);

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);
        
        actionBar=this.getActionBar();       
        ActionBar.LayoutParams params=new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,Gravity.CENTER );
        View view=LayoutInflater.from(this).inflate(R.layout.actionbar, null);
        actionBar.setCustomView(view,params);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        
       ImageButton more=(ImageButton) this.findViewById(R.id.more);
       ImageButton left=(ImageButton) this.findViewById(R.id.left);
       left.setOnTouchListener(new MyTouchListener());
       more.setOnTouchListener(new MyTouchListener());
        return true;
        
    }

}
class MyTouchListener implements OnTouchListener
{

    @Override
    public boolean onTouch(View view, MotionEvent arg1) {
         if(arg1.getAction()==MotionEvent.ACTION_DOWN)
           {
                view.setBackgroundColor(Color.LTGRAY);
           }
           if(arg1.getAction()==MotionEvent.ACTION_UP)
           {
                view.setBackgroundColor(Color.TRANSPARENT);
           }
         return true;
    }
原文地址:https://www.cnblogs.com/zhujiabin/p/4250419.html