要点:共三部分
1.Adding and Handling Actions(添加和处理操作)
2.添加 Up Action
3.Action Views and Action Providers
第一部分:Adding and Handling Actions(添加和处理操作)
1.Add Action Buttons(添加操作按钮)<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_favorite" android:icon="@drawable/ic_heart" android:title="love" app:showAsAction="ifRoom"> </item> <item android:id="@+id/action_settings" android:title="settings" app:showAsAction="never"> </item> </menu></span>2.Respond to Actions 对添加的行为进行反应
<span style="font-size:18px;"> @Override public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case R.id.action_favorite: Toast.makeText(this,"You have added it to your favourite",Toast.LENGTH_SHORT).show(); case R.id.action_settings: Toast.makeText(this,"You are choosing settings",Toast.LENGTH_SHORT).show(); default:return super.onOptionsItemSelected(item); } }</span>
<span style="font-size:18px;"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyChildActivity" android:label="@string/title_activity_child" android:parentActivityName=".MainActivity"></activity> </application></span>
代码如下:
public class MyChildActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_child); Toolbar myChildToolBar = (Toolbar) findViewById(R.id.my_child_toolBar); setSupportActionBar(myChildToolBar); ActionBar ab=getSupportActionBar(); ab.setDisplayHomeAsUpEnabled(true); } }效果图:
<span style="font-size:18px;"><item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_search" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" /></span>其中showAsAction中ifRoom指只要app bar有空间就独立放在app bar上,若没有空间就放在溢出菜单中
<span style="font-size:18px;">@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_activity_actions, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); // Configure the search info and add any event listeners... return super.onCreateOptionsMenu(menu); }</span>
调用静态方法getActionView()来得到action view对象实例
2.添加Action Provider
代码(XML):
<item android:id="@+id/action_share" app:showAsAction="ifRoom" app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
效果图:<img src="http://img.blog.csdn.net/20151206221533114?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="500" alt="" />