Android Development (10) ActionBar & Message & Notification & Broadcasting is all in this article!

Keywords: Mobile Android Fragment Attribute xml

I. Use of ActionBar

Action Bar is the application of the top rectangular bar, the content is generally app icon, app name, menu buttons, tab navigation, etc.

1. Display and hide ActionBar

xml method: Setting the theme attribute of application or activity in manifest can display and hide (theme with NoAction Bar) java method: first, getSupportActionBar gets bar and then calls hide or show method.

2. Add Action Item

It's basically the same step as creating the option menu before, but when defining the item of the menu, there is an additional attribute of showAsAction. The specific value of the attribute is as follows.

[Image upload failed... (image-9955a0-1570973518168)]

  • Examples of menu resource file item
<item
android:id="@+id/search"
android:icon="@drawable/search"
android:title="@string/search"
app:showAsAction="always"></item>
<item

3. Add Action View

To add components to ActionBar Examples of menu resource files are as follows: specific view s can be specified either by class or by layout files

<item
android:id="@+id/search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
></item>
<item
android:id="@+id/img1"
android:title="@string/img1"
app:actionLayout="@layout/img_message"
app:showAsAction="always"></item>

Explain

  • actionLayout, ShowAs Action, etc. are prefixes of app rather than android, and need to introduce corresponding namespaces
  • The setDisplayShowTitleEnabled method of action bar can set the hiding and display of application title.
  • getMenuInflater

4,ActionBar & tab

Implementation of action bar with tab navigation only

Step: create several fragments > implement tablistener > mainactivity add tab to action bar

critical code

//tabListener implements part of the class code
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if(fragment == null){
fragment = Fragment.instantiate(activity,clazz.getName());
fragmentTransaction.add(android.R.id.content,fragment,null);
}
fragmentTransaction.attach(fragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if(fragment != null){
fragmentTransaction.detach(fragment);
}
}
//Main Activity Part Code
ActionBar ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setNavigationMode(ab.NAVIGATION_MODE_TABS);
ab.addTab(ab.newTab().setText("First page").
setTabListener(new MyListener(AFragment.class,MainActivity.this)));
ab.addTab(ab.newTab().setText("Second pages").
setTabListener(new MyListener(BFragment.class,MainActivity.this)));
ab.addTab(ab.newTab().setText("Third pages").
setTabListener(new MyListener(CFragment.class,MainActivity.this)));

5. Realizing Hierarchical Navigation

principle

Active jump returned is not achieved through intent The essence is to set the function of the parent Activity collection Action Bar to achieve return.

critical code

<activity android:name=".BActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"></meta-data>
</activity>
//Subactivity code
if(NavUtils.getParentActivityName(this) != null){
//Here is the icon that opens the return of ActionBar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

2. News-Notification-Broadcasting

1.Toast display message prompt box

### 2. Alert Dialog Implementation Dialog (Four)

  • Common dialog box (with cancel and confirm buttons)

  • tabbed dialog box

  • Radio dialog box

  • Multi-selection dialog box

Tail speech

If there are any mistakes or inappropriateness in this article, readers are welcome to leave a correct message, exchange and learn from each other. The editor is very grateful to you. If you see here, you will give a compliment if you feel that the article is well written! ________ Welcome to comment and discuss! Regular free sharing of technical dry goods.

Posted by Irvin Amoraal on Sun, 13 Oct 2019 08:42:57 -0700