Android UI开发详解之ActionBar(5)

4、可拓展的Item

看到那个放大镜了,就是一个可拓展试的Item,看xml 中的 menu_collapse。
他有俩个属性是Android:actionLayout="@layout/collapsibleview" android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="@layout/collapsibleview" 是点item后拓展成的View
android:showAsAction="collapseActionView"代表他是一个可拓展的Item

Button btnn;
EditText editText;
private ShareActionProvider mShareActionProvider;
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
// 给可折叠的view上的控件添加事件
LinearLayout viewGroup = (LinearLayout) menu.findItem(
R.id.menu_collapse).getActionView();
btnn = (Button) viewGroup.findViewById(R.id.colla_btn);
editText = (EditText) viewGroup.findViewById(R.id.colla_edit);
btnn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
editText.getText() + "~", 0).show();
}
});
}

可拓展的Item同时还支持一个拓展事件。只有在4.0以后的Api提供:

MenuItem menuItem = menu.findItem(R.id.menu_collapse);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"onMenuItemActionExpand", 0).show();
return true;
}
 
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"onMenuItemActionCollapse", 0).show();
return true;
}
});

5、自定义View

mCustomView = getLayoutInflater().inflate(
R.layout.action_bar_display_options_custom, null);
mCustomView.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "自定义view", 0).show();
}
});
actionBar.setCustomView(mCustomView, new ActionBar.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

九、ActionBar标签

1、ActionBar可以现实选项卡供用户切换浏览该Activity中不同的Fragment
2、每个标签都可以是一个图标或文本标题

package com.xys.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity
{
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ActionBar bar = getActionBar();
  //设置为Tab模式
  bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  //新建2个Tab
  ActionBar.Tab tabA = bar.newTab().setText("A Tab");
  ActionBar.Tab tabB = bar.newTab().setText("B Tab");
  //绑定到Fragment
  Fragment fragmentA = new AFragmentTab();
  Fragment fragmentB = new BFragmentTab();
  tabA.setTabListener(new MyTabsListener(fragmentA));
  tabB.setTabListener(new MyTabsListener(fragmentB));
  bar.addTab(tabA);
  bar.addTab(tabB);
 }
 
 protected class MyTabsListener implements ActionBar.TabListener
 {
  private Fragment fragment;
  public MyTabsListener(Fragment fragment)
  {
   this.fragment = fragment;
  }
  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft)
  {
   ft.add(R.id.fragment_place, fragment, null);
  }
  @Override
  public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
   // TODO Auto-generated method stub
   
  }
 }
 
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1c11da2f967898e41b2f0f48714bbf70.html