6. ActionBar详解

ActionBar简介

6. ActionBar详解

 隐藏和显示ActionBar

隐藏ActionBar      getActionBar().hide();

显示ActionBar      getActionBar().show();

 添加和响应Action按钮

6. ActionBar详解

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_hide_show_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick_HideShowActionBar"
android:text="隐藏ActionBar" />
</LinearLayout>
 package cn.eoe.first.actionbar;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast; public class FirstActionBarActivity extends Activity {
private Button hideShowActionBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_first_action_bar);
hideShowActionBar = (Button) findViewById(R.id.button_hide_show_actionbar);
} public void onClick_HideShowActionBar(View view) {
if (getActionBar() == null)
return;
if (getActionBar().isShowing()) {
getActionBar().hide();
hideShowActionBar.setText("显示ActionBar");
} else {
getActionBar().show();
hideShowActionBar.setText("隐藏ActionBar");
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_first_action_bar, menu);
// menu.getItem(2).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
} }
 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item
android:id="@+id/menu_save"
android:icon="@drawable/save"
android:showAsAction="ifRoom|withText"
android:title="保存"/>
<item
android:id="@+id/menu_open"
android:icon="@drawable/open"
android:showAsAction="ifRoom"
android:title="打开"/>
<item
android:id="@+id/menu_search"
android:icon="@drawable/search"
android:showAsAction="always"
android:title="保存"/>
<item
android:id="@+id/menu1"
android:icon="@drawable/ic_launcher"
android:showAsAction="ifRoom"
android:title="菜单项1"/>
<item
android:id="@+id/menu2"
android:icon="@drawable/ic_launcher"
android:showAsAction="ifRoom"
android:title="菜单项2"/> </menu>

 可上下两分的Action

6. ActionBar详解6. ActionBar详解

 package cn.eoe.split.actionbar;

 import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater; public class SplitActionBar extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_split_action_bar);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_split_action_bar, menu);
return true;
}
}
 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item
android:id="@+id/menu_save"
android:icon="@drawable/open"
android:showAsAction="always|withText"/>
<item
android:id="@+id/menu_open"
android:icon="@drawable/open"
android:showAsAction="always"
android:title="打开"/>
<item
android:id="@+id/menu_search"
android:icon="@drawable/search"
android:showAsAction="always"
android:title="保存"/>
<item
android:id="@+id/menu1"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="菜单项1"/>
<item
android:id="@+id/menu2"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="菜单项2"/> </menu>

 收缩和展开Action View 

6. ActionBar详解

 import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnActionExpandListener;
import android.widget.Toast; @SuppressLint("NewApi")
public class ActionViewActivity extends Activity implements
OnActionExpandListener {
// private Menu menu; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_view);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_action_view, menu);
// this.menu = menu;
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setOnActionExpandListener(this);
}
return true;
} @Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(this, "<" + item.getTitle() + ">已经展开", Toast.LENGTH_LONG)
.show(); return true;
} @Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(this, "<" + item.getTitle() + ">已经收缩", Toast.LENGTH_LONG)
.show();
return true;
} }

res/menu/activity_action_view.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

     <item
android:id="@+id/menu_item_edit"
android:actionViewClass="android.widget.EditText"
android:showAsAction="always"
android:title="编辑"/>
<item
android:id="@+id/menu_item_clock"
android:actionLayout="@layout/action_view_clock"
android:icon="@drawable/time" android:showAsAction="always|collapseActionView"
android:title="时间"/>
<item
android:id="@+id/menu_item_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/search"
android:showAsAction="always|collapseActionView"
android:title="搜素"/> </menu>
上一篇:docker 数据管理


下一篇:让Netty入门变得简单