ActionBar
显示隐藏方法:
在布局文件中设置 theme主题
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qf.day15_actionbar_demo1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /> <!-- 换主题 换不同的action样式 android:theme="@style/AppTheme" android:theme="@android:style/Theme.Light" android:theme="@android:style/Theme.Holo" android:theme="@android:style/Theme.Holo.Light" android:theme="@android:style/Theme.Holo.NoActionBar" --> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.qf.day15_actionbar_demo1.MainActivity" android:label="@string/app_name" android:uiOptions="splitActionBarWhenNarrow" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
逻辑代码中
package com.qf.day15_actionbar_demo1; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private ActionBar actionBar; private TextView tv; private int tvSize = 10; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //方法控制没有标题栏 // 要在setContent之前设置否则直接奔溃 //requestWindowFeature(Window.FEATURE_NO_TITLE); //显示 // requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); //获取当前ActionBar actionBar = getActionBar(); //应用图标是否能点击 带一个向左的箭头 监听的ID是android.R.id.home actionBar.setDisplayHomeAsUpEnabled(true); //应用图标是否能点击 不带箭头 //actionBar.setHomeButtonEnabled(true); //是否显示应用程序图标 actionBar.setDisplayShowHomeEnabled(true); } @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); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home://应用程序图标的id finish(); break; case R.id.action_add: tvSize+=10; tv.setTextSize(tvSize); break; case R.id.action_call: tv.setText("拨打电话中...."); break; case R.id.action_camera: tv.append(" "+"美颜拍照"); break; case R.id.action_delete: tv.setText(""); break; default: break; } return super.onOptionsItemSelected(item); } public void MyClick(View v){ //判断actionBar是否正在展示 if(actionBar.isShowing()){ actionBar.hide();//隐藏 }else{ actionBar.show();//显示 } } }