ActionBar
菜单项过多溢出
当创建一个ActionBar拥有太多的Action item以至于没有足够的控件来显示会在屏幕的右上角出现一个溢出菜单项,可以通过它来访问溢出部分菜单。
代码示例:
溢出菜单xml
package com.example.androidactionbartest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuInflater;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); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); return true; } }
添加活动视图
- 我们也可以添加一个视图作为一个Action Item.我们可以通过在xml元素的android:actionLayout属性制定我们希望现实布局资源的ID(例如@layout/mysearchview)
- 我们可以选择添加android:actionViewClass属性分配与完全限定的类名描述我们想要显示的视图(例如android.widget.SearchView)
代码示例:
optionsmenu.xml
java程序:
package com.example.androidactionbartest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuInflater;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); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); return true; } }
ActionBar 标签
-
- ActionBar可以现实选项卡供用户切换浏览该Activity中不同的Fragment
- 每个标签都可以是一个图标或文本标
主程序:
package com.example.androidactionbartest;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.ActionBar;import android.app.ActionBar.Tab;import android.app.Activity;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.MenuInflater;public class MainActivity extends Activity { /** Called when the activity is first created. */ @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); final ActionBar.Tab tabA = bar.newTab().setText("A Tab"); final ActionBar.Tab tabB = bar.newTab().setText("B Tab"); final ActionBar.Tab tabC = bar.newTab().setText("C Tab"); final Fragment fragmentA = new AFragmentTab(); final Fragment fragmentB = new BFragmentTab(); final Fragment fragmentC = new CFragmentTab(); tabA.setTabListener(new MyTabsListener(fragmentA)); tabB.setTabListener(new MyTabsListener(fragmentB)); tabC.setTabListener(new MyTabsListener(fragmentC)); bar.addTab(tabA); bar.addTab(tabB); bar.addTab(tabC); } @SuppressLint("NewApi") protected class MyTabsListener implements ActionBar.TabListener { private Fragment fragment; public MyTabsListener(Fragment fragment) { this.fragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment_place, fragment, null); } @Override public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) { // TODO Auto-generated method stub } @Override public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) { // TODO Auto-generated method stub } } }
其余三个类:
package com.example.androidactionbartest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class AFragmentTab extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); }}
package com.example.androidactionbartest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class BFragmentTab extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_b, container, false); }}
package com.example.androidactionbartest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class CFragmentTab extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_c, container, false); }}
另外fragment_a,fragment_b,fragment_c三个layout xml,此处只是新建这三个,没有什么内容.