Directory (?) [-]
- Home Icon
- Source
- TextView's scrolling
- Returns the main activity or specified activity
Actionbar is introduced for tablets in the Android 3.0 SDK and can be used in phones in 4.0. There are three different forms of tab-and menu-like effects in title: Tabbed Action bar,list Action Bar and standard Action Bar, which we'll demonstrate in a small example.
Home Icon
At the far left of Action Bar is the area of home icon and title, such as The red circle. On the left side of home icon is a left arrow that returns, usually we click on this area and will return to the main activity of the app. The activity in the graph is triggered by the menu of the main activity, which has been used many times before and is not duplicated. The layout of the activity is simple, there is only one textview in LinearLayout, and its code is as follows:
Source
public class HomePressTestCase1 extends activity{
Private TextView TV = null;
private ActionBar bar = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.basic_test);
TV = (TextView) Findviewbyid (R.ID.TEXTVIEWID);
Tv.setmovementmethod (Scrollingmovementmethod.getinstance ());//You can scroll through the mouse when you set too much textview content
Tv.settext ("");
After Android 4.0, the default is not to listen to the Home icon action, to use effectively, you must make the following settings
bar = Getactionbar (); Gets the object of the Actionbar, from which the action bar is a property of the activity
Bar.setdisplayhomeasupenabled (TRUE); Displays the arrow that is returned and listens through onoptionsitemselected () with a resource ID of android.r.id.home.
Bar.sethomebuttonenabled (TRUE); Arrow symbols are not displayed, allowing arrows to be onoptionsitemselected (), but in general, you should give the user an arrow tip.
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
CaseAndroid. R.id.home://For the user to press home icon, this example simply closes the activity and returns to the previous activity, the main activity.
Showinfo ("Home is Press");
Finish ();
return true;
Default
Break
}
return super.onoptionsitemselected (item);
}
protected void Showinfo (String s) {
Tv.settext (s + "\ n" + tv.gettext ());
LOG.D (Getlocalclassname (), s);
}
}
TextView's scrolling
We can set the entire layout to ScrollView, but the TextView itself can be set to scroll, and the related properties in XML are as follows. and make it valid through Setmovementmethod(Scrollingmovementmethod.getinstance ()) in the code.
[HTML]View Plaincopy
- <TextView android:id="@+id/textviewid"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scrollbars="vertical"
- android:scrollbarstyle="Insideoverlay"
- android:scrollbarsize="25dip"
- android:scrollbarfadeduration="0"
- />
Returns the main activity or specified activity
If the previous activity is not the primary activity and we have the hope of returning directly to the main activity, you can do the following:
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case Android. R.id.home:
Showinfo ("Home is pressed");
Intent i = new Intent (this,mainactivity.class);
//"for Flag_activity_clear_top":Assume that the current activity's task stack is mainactivity–> a–> b–> Active (if we press the back key, we return b,a, and finally mainactivity. Now intent to evoke mainactivity and find that it exists on the stack, then it is clear that it and all before it, and after the new activity is aroused, the task stack is mainactivity. Note that by tracking the activity object, a new instance of mainactivity will be generated, not the original, and the original has been killed. For example, currently c-> m->a->b-> is currently open, if M is opened, it is promoted to c->m after execution.
i.addflags (intent.flag_activity_clear_top);
StartActivity (i);
return true;
Default
Break
}
return super.onoptionsitemselected (item);
}
After Android 4.1 (API level 16), there is a more convenient way to set up the returned activity in Androidmanifest.xml, the fragment of XML is as follows:
<activity android:name= ". HomePressTestCase1 "android:label=" @string/test_case_1_home "
android:parentactivityname= "Cn.wei.flowingflying.pro.MainActivity"/>
We only need to set the Parentactivityname, the user presses the home icon area, does not need the manual code processing, can implement above by the set flag the intent code implementation. Java code can be further simplified:
public boolean onoptionsitemselected (MenuItem item) {
if (item.getitemid () = = Android. R.id.home) {
Showinfo ("Home is Press");
}
return super.onoptionsitemselected (item);
}
The example code covered in this blog post can be downloaded in Pro Android Learning: Actionbar Small example.
RELATED Links: My Android development related articles
"Turn" Pro Android Learning Note (48): ActionBar (1): Home icon Area