<span id="Label3"></p><p><p>Action Bar is a good way to provide users with navigation, window position identification, operation Click and other functions in practical application. It appears in the version after Android3.0 (API 11) and can be used in versions after 2.1.</p></p><p><p><span style="font-size: 16pt;"><strong>Add and Hide Action Bar</strong></span></p></p><p><p>In the version after 3.0 (the<span style="color: black;"><span style="font-family: Arial; background-color: white;">android:minsdkversion</span> <span style="color: red;"> <span style="font-family: 宋体; background-color: white;">or</span> <span style="color: black;"> <span style="font-family: Arial; background-color: white;">android:targetsdkversion</span> <span style="font-family: 宋体; background-color: white;">property is set to </span> <span style="font-family: Arial; background-color: white;"></span> <span style="font-family: 宋体; background-color: white;">or</span> </span> higher),</span> </span> The action Bar is added by default in the activity, without adding additional. If you do not want to use the action Bar in your activity, we can do so by setting the subject of the Activity.</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>You can also dynamically hide or show the action Bar during the Program's run:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>Dynamic display or hide will cause the current activity interface to REDRAW. If you need to show or hide frequently, you can use the overlay mode to pin an action bar above the current activity interface, no matter whether it is displayed or hidden, the activity interface is not redrawn.</p></p><p><p><span style="font-size: 16pt;"><strong>Add Item to Action Bar</strong></span></p></p><p><p>The most commonly used item on the action Bar is easy for the user to manipulate, and the unimportant item is hidden in the overflow item. Item is usually predefined in the menu resource file, as Follows:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>When an activity is started, the <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">oncreateoptionsmenu ()</span> method is used to populate the preset item in AB.</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>In the Showasaction property of item, The options have the <span style="color: black;"> <span style="font-family: Arial; background-color: white;"></span> <span style="font-family: 宋体; background-color: white;">following meanings:</span></span></p></p> <ol style="margin-left: 42pt;"> <ol style="margin-left: 42pt;"> <li><span style="font-size: 10pt;">Never: never SHOW. Will only appear in the overflow list.</span></li> <li><span style="font-size: 10pt;">Ifroom: appears in item, but is hidden in the overflow list if it already has 4 or more than 4 Item.</span></li> <li><span style="font-size: 10pt;">Always: it will be displayed regardless of overflow.</span></li> <li><span style="font-size: 10pt;">Withtext:title will be Displayed.</span></li> <li><span style="font-size: 10pt;">Collapseactionview: Extensible Item.</span></li> </ol> </ol><p><p><span style="font-size: 16pt;"><strong>Handling Item Events</strong></span></p></p><p><p>When the user triggers the Item's click event, the <span style="color: #222222;"> <span style="font-family: Arial; background-color: #f9f9f9;"> </span>event is handled by the <span style="color: #33b5e5;"> <span style="font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">onoptionsitemselected ()</span> <span style="color: #222222; font-family: Arial; background-color: #f9f9f9;"> </span> method</span> , as Follows:</span></p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p><span style="font-size: 16pt;"><strong>Return to the previous layer via the app icon</strong></span></p></p><p><p>Clicking the app icon to roll back the previous feature is a completely different two feature from clicking the back button, which is returned back to the previous user interface based on the order of time, instead of the app Hierarchy.</p></p><p><p>To enable the Up-level return feature, you need to call <span style="color: #222222;"> <span style="font-family: Arial; background-color: #f9f9f9;"> </span>the <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">setdisplayhomeasupenabled ()</span> method:</span></p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>In addition, we also need to specify which activity to return up to, depending on the situation, there are two ways:</p></p><p><p>Now that the activity has a unique parent activity a, which can jump from a activity to the current activity, you can specify the parent activity in the manifest file:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>If there is more than one parent activity, the replication <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">getsupportparentactivityintent ()</span> <span style="color: #222222;"> <strong> <span style="font-family: Arial; background-color: #f9f9f9;"> </span> <span style="font-family: 宋体; background-color: #f9f9f9;">and</span> </strong> <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;"> Oncreatesupportnavigateuptaskstack ()</span> method to</span> specify which activity to return to.</p></p><p><p>When the user is working in several layers of the same app, click up for fallback, and the system calls the <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">getsupportparentactivityintent ()</span> method. Because there may be more than one parent activity, you can return a intent that starts the parent activity in this Method.</p></p><p><p>The <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">oncreatesupportnavigateuptaskstack ()</span> method is called when the user clicks up to roll back when they are working in a multi-tiered activity across the app. At this point, you need to pass the <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">taskstackbuilder</span> <span style="color: #222222; font-family: Arial; background-color: #f9f9f9;"> </span> to the method and construct the corresponding fallback stack.</p></p><p><p>Even if the <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">getsupportparentactivityintent ()</span> method is used to specify that the fallback action in this app is not to be replicated <span style="color: #258aaf; font-family: Courier New; font-size: 10pt; background-color: #f9f9f9;">Oncreatesupportnavigateuptaskstack ()</span> method by specifying the default activity in the manifest File.</p></p><p><p></p></p><p><p><span style="font-size: 16pt;"><strong>Add Action View</strong></span></p></p><p><p>Adding the Action View widget to the action bar helps the user to complete functions without changing the current activity or fragment, such as searching, and so On.</p></p><p><p>Specify the layout file or class that you want to use by declaring the <span style="color: #006600; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">actionlayout</span> <span style="font-family: Arial; background-color: white;"> </span> <span style="font-family: 宋体; background-color: white;">or</span> <span style="color: #006600; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">actionviewclass</span> property in the menu File's item, such as adding a search control:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>After you add the menu file, you can get and configure the action view in the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">oncreateoptionsmenu ()</span> method:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>The action view is obtained by passing the obtained MenuItem item to the Getactionview method, and if it is above the 11 version, it is called directly getactionview:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>You can collapse the action view by specifying the <span style="color: #006600; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">Collapseactionview</span> property, and then it will appear when you click it, and the system will automatically callback the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">onoptionsitemselected ()</span> method. If you return the result to true (meaning that you have handled the event and do not continue to pass the event), the action view cannot be Expanded.</p></p><p><p></p></p><p><p><span style="font-size: 16pt;"><strong>Add Action Provider</strong></span></p></p><p><p>Similar to action view, action provider uses a custom layout to replace a fixed action Button. It puts a series of actions into a submenu and clicks it to SHOW.</p></p><p><p>Because action provider defines its own action behavior, there is no need to listen for click events in the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">onoptionsitemselected ()</span> method.</p></p><p><p>Specify the provider class to use by specifying the <span style="color: #006600; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">Actionviewclass</span> property of the item, or you can customize the provider by inheriting and extending the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">actionprovider</span> class. Android has pre-defined some provider, such as <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Shareactionprovider</span>, that enable developers to share Features:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>At this point, we need to specify the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Intent</span>to be used for sharing. here, we need to get to the MenuItem and pass to the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Getactionprovider ()</span> method to get to Provider. The <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">setshareintent ()</span> method is used to set the intent used to initialize the share action, but the User's context is changing, so the intent to be shared must be updated in a timely manner. Examples are as Follows:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>The Shareactionprovider is responsible for handling all user interaction, so there is no need to handle the listener event in the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">onoptionsitemselected ()</span> method. By default, Shareactionprovider Records how often the user selects each item and sorts all item by this rank from high to low.</p></p><p><p><span style="font-size: 16pt;"><strong>Custom provider</strong></span></p></p><p><p>We can customize the provider by inheriting the extended <span style="color: #039be5; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">actionprovider</span> <span style="font-family: Arial; background-color: white;"> </span> class, and we need to override the following methods:</p></p> <ul style="margin-left: 42pt;"> <ul style="margin-left: 42pt;"> <li><span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Actionprovider ()</span> <span style="font-size: 10pt;">: The context of the app is passed into the construction method and should be stored as a global variable. </span></li> <li><span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Oncreateactionview (MenuItem)</span> <span style="font-size: 10pt;">: In this method, define the view to be displayed in item, complete the operation of initializing view and event listening processing, and so on:</span></li> </ul> </ul><p style="text-align: center;"><p style="text-align: center;"></p></p> <ul style="margin-left: 42pt;"> <ul style="margin-left: 42pt;"> <li><span style="color: #039be5; font-family: Consolas; font-size: 10pt;">onperformdefaultaction ()</span> <span style="font-size: 10pt;">: This method will be called when the menu option is selected from OVERFLOW. Provider should provide a default action for the menu Item. </span></li> </ul> </ul><p><p>however, if provider provides a submenu through the <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">onpreparesubmenu ()</span> method, The submenu will be displayed even if the provider is placed in the OVERFLOW.</p></p><p><p></p></p><p><p><span style="font-size: 16pt;"><strong>Add Navigation tabs</strong></span></p></p><p><p>Adding navigation to the Action Bar tabs helps users easily switch views, and these navigation tabs are well suited to screens of various models.</p></p><p><p>First of all, we need to have a fragment viewgroup on the interface, the most basic approach is as follows:</p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li><span style="font-size: 10pt;">Implement</span> <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Actionbar.tablistener</span> <span style="font-size: 10pt;">the interface is used to respond to tabs Events. </span></li> <li><span style="font-size: 10pt;">Instantiate the tab you want to add and set the response event for it.</span></li> <li><span style="font-size: 10pt;">through</span> <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">AddTab ()</span> <span style="font-size: 10pt;">method to add an instantiated tab. </span></li> </ul></li> </ul> </ul><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>In this example, when the tab is clicked, listener instantiates the corresponding fragment and joins the Layout. The next step is to create the tab object and add it to the action bar. In addition, you need to set <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">Setnavigationmode (navigation_mode_tabs)</span>to make the TABS visible. The following shows how to use Listener:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>When the activity stops, you can record the current tab position (<span style="font-family: Arial; background-color: white;"> </span> <span style="color: #039be5; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">getselectednavigationindex ()</span>) so that when you open the activity again, you can see the tab at the Close.</p></p><p><p>We can also use Viewpager to manage fragment, and we just need to tell Viewpager which fragment is currently selected in the method <span style="color: #039be5; font-family: Consolas; font-size: 10pt;">ontabselected ()</span> , so that gesture swipe to switch view is Enabled.</p></p><p><p></p></p><p><p><span style="font-size: 16pt;"><strong>Add a Drop-down navigation</strong></span></p></p><p><p>Drop-down navigation is typically used to place navigation items that must be, but are not frequently used. Adding a Drop-down navigation typically requires the following steps:</p></p> <ul> <li style="list-style-type: none;"> <ul> <ul> <li> <span style="font-size:10pt;"> Create a </span> <span style="color: #039be5; font-family:consolas; font-size: 10pt; ">spinneradapter </span> <span style="font-size:10pt;" is used to provide selectable list items. < span> </span></li> <li> <span style="font-size:10pt;" "implementation < span> <span style=" color: #039be5; font-family:consolas; font-size:10pt; ">actionbar.onnavigationlistener </span> <span style="font-size:10pt;"> interface, listen for selection events. </span> </span></li> <li> <span style="font-size:10pt;" "in activity < span> <span style=" color: #039be5; Font-family:consolas ; font-size:10pt; ">oncreate () </span> <span style="font-size:10pt;" in settings < span> <span style="color: #039be5; font-family:consolas; font-size:10pt; ">setnavigationmode (navigation_mode_list) </span> <span style="font-size:10pt;" to make the drop-down list available. < span> </span></span></span></li> <li> <span style="font-size:10pt;" through < span> <span style="color: #039be5; font-family:consolas; font-size:10pt; ">setlistnavigationcallbacks () </span> <span style="font-size:10pt;" "method set the callback function for drop-down list: < span> </span></span></li> </ul> </ul></li> </ul><pre><pre><code><span style="color: black; font-family: Consolas; font-size: 10pt;"> actionBar<span style="color: #666600;">.<span style="color: black;">setListNavigationCallbacks<span style="color: #666600;">(<span style="color: black;">mSpinnerAdapter<span style="color: #666600;">,<span style="color: black;"> mNavigationCallback<span style="color: #666600;">);</span></span></span></span></span></span></span></span></code></pre></pre><p><p><span style="font-family: Consolas; font-size: 10pt;">Spinneradapter</span> is an interface used to provide data for spinner. At the same time, Android also offers other adapter that can be inherited, such as Arrayadapter or simplecursoradapter. For example, create a adapter that uses a string array as the data source through Arrayadapter:</p></p><p> <code><code> <span style="color: #660066; font-family:consolas; font-size:10pt; >spinneradapter<span style=" color:black; "> Mspinneradapter <span style="color: #666600;">=<span style="color:black;"><span style="color: #660066;"> Arrayadapter<span style="color: #666600;">.<span style="color: #666600;" createfromresource<span> (<span style="color: #000088;">this <span style="color: #666600;" ", < span> </span> </span> </span> </span> </span> </span> </span> </span> </code></code><em id="__mceDel"><code><code> <span style="color: #660066; font-family:consolas; font-size:10pt; ><span style=" color:black; "><span style="color: #666600;"><span style="color:black;"><span style="color: #660066;"><span style="color: #666600;"><span style="color:black;"> <span style="color: #666600;"><span style="color: #000088;"><span style="color: #666600;"><span style="color:black;">r <span style="color:black;" . <span>array<span style="color: #666600;">.<span style="color:black;"> Action_list<span style="color: #666600;">,<span style="color:black;"> Android<span style="color: #666600;">.< Span style= "color:black;" >r<span style="color: #666600;">.<span style="color: #666600;" layout<span>.<span style="color: #666600;" simple_spinner_dropdown_item<span ); < span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span> </code></code></em></p><pre><pre><em id="__mceDel"><em id="__mceDel"><code></code></em></em></pre></pre><p><p><span style="color: #039be5; font-family: Consolas; font-size: 10pt; background-color: #f7f7f7;">Actionbar.onnavigationlistener</span> <span style="font-family: Arial; background-color: white;"></span> the <span style="font-family: 宋体; background-color: white;">interface needs to implement the response event for the selected item, and the following example adds a</span> <span style="font-family: Arial; background-color: white;">fragment</span> <span style="font-family: 宋体; background-color: white;">to the Layout:</span></p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p><p>Android Action Bar</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service