actionbar
来源:互联网 发布:linux vi 序号 编辑:程序博客网 时间:2024/06/07 18:47
- 提供一个专门的空间给你的应用身份和指示应用程序用户的位置。
- 突出和显示重要一些的操作(such as Search).
- 在应用程序中支持统一的导航和视图切换(标签或下拉列表).
- 创建activity 并且继承
ActionBarActivity
. - Use (or extend) one of the
Theme.AppCompat
themes for your activity. For example:<activity android:theme="@style/Theme.AppCompat.Light" ... >
tip:api 11以上
action bar包含所有activities都使用主题Theme.Holo
(或者他们的子类), which is the default theme when either the targetSdkVersion
or minSdkVersion
attribute is set to "11"
or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar
.
Removing the action bar
ActionBar actionBar = getSupportActionBar()
;actionBar.hide();
添加 Action ItemsOn API level 11 or higher
获取
ActionBar
是通过getActionBar()
方法当action bar隐藏了,系统会调整布局填充剩余的屏幕空间,全屏显示.。如果要继续显示就调用
show()
..小心隐藏和删除actionbar使您activity布局以占actionbar所占用的空间。如果要经常隐藏和显示actionbar,最好用overlay mode ,这个模式可以解决占用空间问题,使用这个模式的方式是自定义主题,设置
windowActionBarOverlay
的值是true
the system populates the action items by calling your activity'sonCreateOptionsMenu()
method. Use this method to inflate a menu resource that defines all the action items. For example, here's a menu resource defining a couple of menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search"/> <item android:id="@+id/action_compose" android:icon="@drawable/ic_action_compose" android:title="@string/action_compose" /></menu>
Then in your activity's onCreateOptionsMenu()
method, inflate the menu resource into the given Menu
to add each item to the action bar:
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu);}
To request that an item appear directly in the action bar as an action button, include showAsAction="ifRoom"
in the <item>
tag. For example:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="ifRoom" /> ...</menu>
If your menu item supplies both a title and an icon—with the title
and icon
attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText"
to the showAsAction
attribute. For example:
<item yourapp:showAsAction="ifRoom|withText" ... />
Handling clicks on action items
When the user presses an action, the system calls your activity's onOptionsItemSelected()
method. Using theMenuItem
passed to this method, you can identify the action by calling getItemId()
. This returns the unique ID provided by the <item>
tag's id
attribute so you can perform the appropriate action. For example:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.action_search: openSearch(); return true; case R.id.action_compose: composeMessage(); return true; default: return super.onOptionsItemSelected(item); }}
Using split action bar
- Add
uiOptions="splitActionBarWhenNarrow"
to each<activity>
element or to the<application>
element. This attribute is understood only by API level 14 and higher (it is ignored by older versions). - To support older versions, add a
<meta-data>
element as a child of each<activity>
element that declares the same value for"android.support.UI_OPTIONS"
.
For example:
<manifest ...> <activity uiOptions="splitActionBarWhenNarrow" ... > <meta-data android:name="android.support.UI_OPTIONS" android:value="splitActionBarWhenNarrow" /> </activity></manifest>
Using split action bar also allows navigation tabs to collapse into the main action bar if you remove the icon and title (as shown on the right in figure 3). To create this effect, disable the action bar icon and title withsetDisplayShowHomeEnabled(false)
and setDisplayShowTitleEnabled(false)
.
To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled()
. For example:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); ...}Specify the parent activity in the manifest file.
This is the best option when the parent activity is always the same. By declaring in the manifest which activity is the parent, the action bar automatically performs the correct action when the user presses the Upbutton.
Beginning in Android 4.1 (API level 16), you can declare the parent with the parentActivityName
attribute in the <activity>
element.
To support older devices with the support library, also include a <meta-data>
element that specifies the parent activity as the value for android.support.PARENT_ACTIVITY
. For example:
<application ... > ... <!-- The main/home activity (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 API level 7+ --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity></application>
Once the parent activity is specified in the manifest like this and you enable the Up button withsetDisplayHomeAsUpEnabled()
, your work is done and the action bar properly navigates up.
- ActionBar
- ActionBar
- ActionBar
- Actionbar
- ActionBar
- actionbar
- actionBar
- actionbar
- ActionBar
- ActionBar
- ActionBar
- ActionBar
- ActionBar
- ActionBar
- actionbar
- ActionBar()
- actionbar
- ActionBar
- Soot学习总结
- .Net4.0的网站在IE10、IE11出现“__doPostBack未定义”的解决办法
- kali更新2.0发现以前的源不能用解决方案
- eclispe 快捷键大全
- RPCA图像处理中的矩阵重建算法及其应用
- actionbar
- pycaffe学习
- performSelector最多只能传两个参数
- 不同库 不同表 不同字段 数据同步 触发器
- 代码注释(code comments)
- Android中编辑文本时使用TextWatcher和InputFilter.Length
- svn回退到某个版本
- 判断一个树是否为另一个树的子树
- 如何去年gedit编辑文件时生成的临时文件