?? Pro Android Learning Note (48): ActionBar (1): Home icon AreaMarch 10, 2013 ? Integrated? A total of 3256 words? SizeXiao Zhong da ? Comments Off
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 the sample.
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 diagram is triggered by the main activity menu. It has been used many times before and is not repeated. The layout of the activity is very easy, there is only one textview in the LinearLayout, its code such as the following:
Source Code
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 ()); //TextView can be scrolled with mouse when too much content is set
Tv.settext ("");
//After Android 4.0. The default is not to listen for home icon action. To use effectively. The following settings must be made
Bar = Getactionbar (); //Get Actionbar object, from this method also know that action Bar is a property of activity
Bar.setdisplayhomeasupenabled (TRUE);
Displays the arrow that is returned and listens through onoptionsitemselected (). Its resource ID is android.r.id.home.
//Bar.sethomebuttonenabled (TRUE); //Do not display arrow symbols, agree to onoptionsitemselected () arrows. But the general case. Or you should give an arrow to the user.
}
@Override
Public boolean Onoptionsitemselected (MenuItem Item) {
switch (Item.getitemid ( ) {
case Android. R.id.home : //The user presses home icon, this example only needs to close activity. You can return to the previous activity, which is 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 are able to set the entire layout to ScrollView. But TextView itself is able to set the scrolling, related properties in XML such as the following. and make it valid through setmovementmethod(scrollingmovementmethod.getinstance () ) in the code.
<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
Suppose the previous activity is not the main activity. And we have the hope of returning directly to the main activity, which can be handled as follows:
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": if the task stack for ACTIVITY now is mainactivity–> a–> b–> Current activity (if we press the back key, we return to B,a and finally to mainactivity.) Now intent to evoke mainactivity. Now that the stack exists, it will be clear and all that is before it. After the new activity is aroused, the task stack is mainactivity. Note that byThe object that tracks the activity will produce a new instance of the mainactivity. Not the original, the original has been killed. Another example is the current c->
M->a->b-> Currently, if M is turned on, it runs after. The promotion is c->m.
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 of handling, set the return activity in Androidmanifest.xml can be. Fragments of XML such as the following:
<activity android:name= ". HomePressTestCase1 "android:label=" @string/test_case_1_home "
android:parentactivityname= "Cn.wei.flowingflying.pro.MainActivity"/>
We just need to set parentactivityname, the user presses the home icon area. It is possible to implement the intent code above by setting the flag without the need for manual code processing.
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);
}
Pro Android Learning Note ActionBar (1): Home icon Area