适用于Android设计风格的ActionBar

来源:互联网 发布:windows输入法不见了 编辑:程序博客网 时间:2024/06/08 06:06

由于Android自3.0以后,引入了ActionBar的概念,即要取代menu的作用,所以本示例为简单操作ActionBar的代码,供参考
源码:https://github.com/johannilsson/android-actionbar

main.xml外部引用原始文档
 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <com.markupartist.android.widget.ActionBar        android:id="@+id/actionbar"        style="@style/ActionBar"        />    <ScrollView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_marginTop="20dip">        <Button            android:id="@+id/start_progress"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Start Progress" />        <Button            android:id="@+id/stop_progress"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Stop Progress" />        <Button            android:id="@+id/remove_actions"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Clear actionbar" />        <Button            android:id="@+id/add_action"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Add action" />        <Button            android:id="@+id/remove_action"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Remove action" />        <Button            android:id="@+id/remove_share_action"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Remove share action" />    </LinearLayout>    </ScrollView></LinearLayout>
MainActivity.java外部引用原始文档
 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
package com.example.actionbar_clustermenu;import android.os.Bundle;import android.app.ActionBar;import android.app.ActionBar.OnNavigationListener;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.SpinnerAdapter;public class MainActivity extends Activity{private ActionModeHandler mActionModeHandler;private static final int MENU_PREFERENCES = 31;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 生成一个SpinnerAdapter        SpinnerAdapter adapter = ArrayAdapter.createFromResource(this, R.array.student, android.R.layout.simple_spinner_dropdown_item);        // 得到ActionBar        ActionBar actionBar = getActionBar();        // 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);        // 为ActionBar设置下拉菜单和监听器        actionBar.setListNavigationCallbacks(adapter, new DropDownListenser());                mActionModeHandler = new ActionModeHandler(this);    }    /**     * 实现 ActionBar.OnNavigationListener接口     */    class DropDownListenser implements OnNavigationListener    {        // 得到和SpinnerAdapter里一致的字符数组        String[] listNames = getResources().getStringArray(R.array.student);        /* 当选择下拉菜单项的时候,将Activity中的内容置换为对应的Fragment */        public boolean onNavigationItemSelected(int itemPosition, long itemId)        {                        return true;        }    }@Overridepublic boolean onPrepareOptionsMenu(Menu menu) {super.onPrepareOptionsMenu(menu);menu.clear();menu.add(0, MENU_PREFERENCES, 0, R.string.action_mode).setIcon(                android.R.drawable.ic_menu_preferences);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(MENU_PREFERENCES == item.getItemId()){ mActionModeHandler.startActionMode();}return super.onOptionsItemSelected(item);}                }
ActionModeHandler.java外部引用原始文档
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.actionbar_clustermenu;import android.app.Activity;import android.view.ActionMode;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.PopupMenu.OnMenuItemClickListener;import com.example.actionbar_clustermenu.CustomMenu.DropDownMenu;public class ActionModeHandler implements ActionMode.Callback {    private Activity  mActivity;    private DropDownMenu mSelectionMenu;    private boolean selectAllState = false;    public ActionModeHandler(Activity activity) {    mActivity = activity;    }    public ActionMode startActionMode() {        final ActionMode actionMode = mActivity.startActionMode(this);        View customView = LayoutInflater.from(mActivity).inflate(                R.layout.action_mode, null);        actionMode.setCustomView(customView);        CustomMenu customMenu = new CustomMenu(mActivity);        mSelectionMenu = customMenu.addDropDownMenu(                (Button) customView.findViewById(R.id.selection_menu),                R.menu.selection);        updateSelectionMenu();        customMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {            public boolean onMenuItemClick(MenuItem item) {                return onActionItemClicked(actionMode, item);            }        });        return actionMode;    }    public void setTitle(String title) {        mSelectionMenu.setTitle(title);    }    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {        if (item.getItemId() == R.id.action_select_all) {            updateSelectionMenu();        }        return true;    }    private void updateSelectionMenu() {        // update title        setTitle("oh yeah");        if(selectAllState){        selectAllState = false;        }else {        selectAllState = true;        }        // For clients who call SelectionManager.selectAll() directly, we need to ensure the        // menu status is consistent with selection manager.        MenuItem item = mSelectionMenu.findItem(R.id.action_select_all);        if (item != null) {            if (selectAllState) {                item.setChecked(true);                item.setTitle(R.string.deselect_all);            } else {                item.setChecked(false);                item.setTitle(R.string.select_all);            }        }    }    public boolean onCreateActionMode(ActionMode mode, Menu menu) {        MenuInflater inflater = mode.getMenuInflater();        inflater.inflate(R.menu.activity_main, menu);        return true;    }    public void onDestroyActionMode(ActionMode mode) {        //leave current UI to before activity     }    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {        return true;    }}
9999999999999999.png外部引用原始文档
原创粉丝点击