【Android文档】Training-------Adding the Action Bar
转载请注明出处 :http://blog.csdn.net/chziroy/article/details/44171197
本文主要翻译android开发官网Training中关于ActionBar的几篇文章
Setting up the Action Bar
Support Android 3.0 and Above Only
<manifest ... > <uses-sdk android:minSdkVersion="11" ... /> ... </manifest>
注意,如果你自定义一个主题,该自定义主题需要继承自Theme.Holo。好了,通过以上设置,Theme.Holo主题就被设置到你整个app的所有activity中了。
Adding Action Buttons
Specify the Actions in XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showAsAction="ifRoom" /> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /> </menu>此处,当action bar控件足够时,Search action按钮将直接呈现在action bar上,而如果Setting action会一直隐藏在overflow中。默认情况所有action都是在overflow中的,但是官方建议最好自己为各个item声明位置。
Add the Actions to the Action Bar
如何将上面声明的menu items添加到action bar呢?你可以实现activity的onCreateOptionMenu()来将某个menu资源文件inflate到该方法的Menu参数对象中。如下:@Override public 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); }
Respond to Action Buttons
@Override public 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_settings: openSettings(); return true; default: return super.onOptionsItemSelected(item); } }
Add Up Button for Low-level Activities
<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>
如果是在4.1及以上版本,则只需使用parentActivityName,以上的meta-data是针对4.0及之下的版本。但是为了兼容,其实可以一直都把两个一起写。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_displaymessage); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // If your minSdkVersion is 11 or higher, instead use: // getActionBar().setDisplayHomeAsUpEnabled(true); }
Styling the Action Bar
Use an Android Theme
- Theme.Holo:一个褐色的主题
- Theme.Light:一个白色的主题
<application android:theme="@android:style/Theme.Holo.Light" ... />
你也可以使用黑色头部,白色主题的主题:Theme.Holo.Light.DarkActionBar
Customize the Background
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> </style> </resources>
然后将该主题应用到整个app中:
<application android:theme="@style/CustomActionBarTheme" ... />
使用了navigation tabs 或者 split action bar,的action bar,也可以使用 backgroundStacked 和 backgroundSplit指定主题样式。
Customize the Text Color
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.Holo"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item> <item name="android:actionMenuTextColor">@color/actionbar_text</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.Holo.ActionBar"> <item name="android:titleTextStyle">@style/MyActionBarTitleText</item> </style> <!-- ActionBar title text --> <style name="MyActionBarTitleText" parent="@style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_text</item> </style> <!-- ActionBar tabs text styles --> <style name="MyActionBarTabText" parent="@style/Widget.Holo.ActionBar.TabText"> <item name="android:textColor">@color/actionbar_text</item> </style> </resources>
Customize the Tab Indicator
Overlaying the Action Bar
Enable Overlay Mode
<resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"> <item name="android:windowActionBarOverlay">true</item> </style> </resources>
Specify Layout Top-margin
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?android:attr/actionBarSize"> ... </RelativeLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。