新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ActionBarItem如何在Android应用中使用

这篇文章将为大家详细讲解有关ActionBar Item如何在Android应用中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

站在用户的角度思考问题,与客户深入沟通,找到龙城网站设计与龙城网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站设计、企业官网、英文网站、手机端网站、网站推广、空间域名、雅安服务器托管、企业邮箱。业务覆盖龙城地区。

1、在Androidmanifest.xml里面加上

或者

要使版本号在3.0以上,这样系统就自动把Menu放在ActionBar上面。

2、准备一个menu.xml,以便加载。其实这就是一个传统的menu布局,只是多了android:showAsAction="ifRoom|withText"这个属性,ifRoom的意思就是说只要ActionBar上有空间,就把该Item显示出来,否则就坠在后面。

<?xml version="1.0" encoding="utf-8"?>

  
  
  
  
  
    
      
      
    
  

3、Activity中的代码:

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;
/**
 * This demonstrates idiomatic usage of the Action Bar. The default Honeycomb theme
 * includes the action bar by default and a menu resource is used to populate the
 * menu data itself. If you'd like to see how these things work under the hood, see
 * ActionBarMechanics.
 */
public class ActionBarUsageActivity extends Activity implements OnQueryTextListener {
  TextView mSearchText;
  int mSortMode = -1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSearchText = new TextView(this);
    setContentView(mSearchText);
  }
  //和加载传统的menu一样,重写onCreateOptionsMenu方法
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actions, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(this);
    return true;
  }
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    if (mSortMode != -1) {
      Drawable icon = menu.findItem(mSortMode).getIcon();
      menu.findItem(R.id.action_sort).setIcon(icon);
    }
    return super.onPrepareOptionsMenu(menu);
  }
  //和相应传统的menu一样,重写onOptionsItemSelected方法
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
    return true;
  }
  // This method is specified as an onClick handler in the menu xml and will
  // take precedence over the Activity's onOptionsItemSelected method.
  // See res/menu/actions.xml for more info.
  public void onSort(MenuItem item) {
    mSortMode = item.getItemId();
    // Request a call to onPrepareOptionsMenu so we can change the sort icon
    invalidateOptionsMenu();
  }
  // The following callbacks are called for the SearchView.OnQueryChangeListener
  // For more about using SearchView, see src/.../view/SearchView1.java and SearchView2.java
  public boolean onQueryTextChange(String newText) {
    newText = newText.isEmpty() ? "" : "Query so far: " + newText;
    mSearchText.setText(newText);
    return true;
  }
  public boolean onQueryTextSubmit(String query) {
    Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
    return true;
  }
}

关于ActionBar Item如何在Android应用中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享文章:ActionBarItem如何在Android应用中使用
分享地址:http://www.wjwzjz.com/article/jiohco.html
在线咨询
服务热线
服务热线:028-86922220
TOP