AndroidActionBar
- API版本在3.0(11)或更高,需要使用Theme.Holo创建actionbar。
- API版本需要兼容2.1(7),需要继承ActionBarActivity,并设置该activity的Theme为Theme.Appcompat.Light。
- API版本需要兼容2.1,ActionButton中的showAsAction属性的namespace为android会无效,建议namespace为appname。
- 关于actionbar的上部导航添加返回图标的事例代码如下:
- 设置返回按钮显示时,3.0以下api是getSupportActionBar.setDisplayHomeAsUpEnabled,3.0以上就是getAction.setDisplayHomeAsUpEnabled.
- 学习链接:http://wear.techbrood.com/training/basics/actionbar/index.html
-
<application ... > ... <!-- The main/home activity (it has no parent activity) --> <activity android:name="com.example.myfirstapp.MainActivity" ...> ... </activity> <!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>
SupportActionBar(转载)
1.设置加载条和加载圈
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 // Bar和下面的Activity背景都为白色 4 setTheme(R.style.Theme_Sherlock_Light); 5 //圆形加载必须在setContextView之前调用 6 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 7 //条形加载必须在setContextView之前调用 8 requestWindowFeature(Window.FEATURE_PROGRESS); 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 //启用圆形加载 12 setSupportProgressBarIndeterminateVisibility(false); 13 //启用条形加载 14 setSupportProgressBarVisibility(true);setSupportProgress(进度); 15 }
2.是否显示添加的View
1 /** 2 * 添加view,然后设置是否显示 3 */ 4 getSupportActionBar().setCustomView(R.layout.custom_view); 5 getSupportActionBar().setDisplayShowCustomEnabled(false);
3.属性设置
1 /** 2 * 显示ActionBar 3 */ 4 getSupportActionBar().show(); 5 /** 6 * 隐藏ActionBar 7 */ 8 getSupportActionBar().hide();
4.Navigation设置
1 /** 2 * 显示标准的ActionBar 3 */ 4 getSupportActionBar().setNavigationMode( 5 ActionBar.NAVIGATION_MODE_STANDARD);
1 /** 2 * 显示有list的ActionBar 3 */ 4 getSupportActionBar().setNavigationMode( 5 ActionBar.NAVIGATION_MODE_LIST);
1 /**为NAVIGATION_MODE_LIST添加list 2 * R.array.locations文字数组 3 * R.layout.sherlock_spinner_item默认就OK 4 * R.layout.sherlock_spinner_dropdown_item默认就OK 5 */ 6 Context context = getSupportActionBar().getThemedContext(); 7 ArrayAdapter<CharSequence> listAdapter = ArrayAdapter 8 .createFromResource(context, R.array.locations, 9 R.layout.sherlock_spinner_item); 10 listAdapter 11 .setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setListNavigationCallbacks(listAdapter, ActionBar.OnNavigationListener);
其中的ActionBar.OnNavigationListener接口用于监听点击事件
/** * 显示有tab的ActionBar */ getSupportActionBar().setNavigationMode( ActionBar.NAVIGATION_MODE_TABS);
1 /** 2 * 添加tabitem 3 */ 4 ActionBar.Tab newTab = getSupportActionBar().newTab(); 5 //为item添加子布局 6 //newTab.setCustomView(R.layout.tab_custom_view); 7 //为item添加icon 8 //newTab.setIcon(R.drawable.ic_title_share_default); 9 //为item添加标题 10 newTab.setText("Text!"); 11 //为item添加监听 12 newTab.setTabListener(实现这个接口ActionBar.TabListener); 13 //添加到Actionb中 14 getSupportActionBar().addTab(newTab);
1 /** 2 * 隐藏/显示Tittle 3 */ 4 getSupportActionBar().setDisplayShowTitleEnabled(true);
1 /** 2 * 隐藏/显示Custom 3 */ 4 getSupportActionBar().setDisplayShowCustomEnabled(true);
1 /** 2 * 添加子标题 3 */ 4 getSupportActionBar().setSubtitle( 5 "The quick brown fox jumps over the lazy dog.");
1 /** 2 * 隐藏/显示返回箭头 3 */ 4 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
1 /** 2 * 隐藏/显示功能图片 3 */ 4 getSupportActionBar().setDisplayShowHomeEnabled(true);
1 /** 2 * 设置使用activity的logo还是activity的icon 3 * true为logo 4 */ 5 getSupportActionBar().setDisplayUseLogoEnabled(true);
搜索
复制