zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    TOEFL资料 280多个
    Eclipse搭建J2ME开发环境
    Session.Abandon和Session.Clear有何不同
    进程之同步、互斥PV操作笔记
    Windows Mobile 6.5 实现联系人分组显示
    关于数据库的版本控制
    xhtml的布局,满屏,高度自适应
    MOSS 项目模板
    DNN中与模块相关的信息
    J2EE学习笔记
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4250419.html
Copyright © 2011-2022 走看看