Original: http://blog.csdn.net/lihongxun945/article/details/48951199
We can already add some components such as buttons in one activity. Because the screen of the phone is very small, many times we will need to use the toolbar, the drop-down menu and other ways to save space.
Android provides powerful support for toolbar buttons.
Add a toolbar button
We now MainActivity
add a search button to it. Add a button to do these three things
One, res/menu/activity_main.xml
add a button in the configuration. This file is already in the auto-generated project, and a Settings button is created automatically, so we can just add a search button
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_search" android:icon="@drawable/ic_search_white_24dp" 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>
Note that we have used the drawable
and string
two resources, we need to create a.
Second, refer to the MainActivity
configured button in the
MainActivity
has already declared a method in onCreateOptionsMenu
, actually does not have to change at all:
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true;}
Third, bind the event to the button
Here we have a onOptionsItemSelected
method, just need to add a bit of event monitoring on the line
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement switch (id) { case R.id.action_search: Toast.makeText(this, "你点击了搜索", Toast.LENGTH_SHORT).show(); case R.id.action_settings: Toast.makeText(this, "你点击了设置", Toast.LENGTH_SHORT).show(); } return super.onOptionsItemSelected(item);}
Add Back button
Many times, we need to MainActivity
add a return button to the inactive activity so that the user can return to the previous activity via the return button. Android provides default support for the return button, and we just need to configure it, without having to write back the logic code.
Only two steps are required to implement the function of the return button. Here we want to be able to ProfileActivity
add a return button in, click Back MainActivity
in
One, add the statement of the AndroidManifest.xml
parent activity in the activity's statement:
<activity android:name=".ProfileActivity" android:label="@string/title_activity_profile" android:parentActivityName=".MainActivity"> <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> <intent-filter> <action android:name="com.lihongxun.Profile" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Note that if you want to support 4.0 and the following devices need to add meta-data
that line, or you just need to add the android:parentActivityName=".MainActivity”
Second, ProfileActivity
enable the return button in. onCreate
add a line of code to the function:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And then run it and try again.
Three-covering toolbar
By default, the top toolbar is space-intensive, and he will squeeze the content area down. Sometimes you might want it to be covered in the content area, especially if you want to be able to dynamically display the hidden toolbar.
You only need to styles.xml
declare the use of the Override toolbar in the
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="android:windowActionBarOverlay">true</item> <!-- Support library compatibility --> <item name="windowActionBarOverlay">true</item></style>
The two lines in the middle are item
declarations of the overlay toolbar, and note that the second line is compatible with the library.
Android Learning Note Four: Creating a toolbar button