Actionbar custom view layout file action_bar.xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "match_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">
<TextView
Android: id = "@ + id/action_bar_title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# EEA9B8"
Android: text = "@ string/action_bar_title"/>
<Spinner
Android: id = "@ + id/action_bar_spinner"
Android: layout_width = "wrap_content"
Android: layout_height = "match_parent">
</Spinner>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "match_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">
<TextView
Android: id = "@ + id/action_bar_title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# EEA9B8"
Android: text = "@ string/action_bar_title"/>
<Spinner
Android: id = "@ + id/action_bar_spinner"
Android: layout_width = "wrap_content"
Android: layout_height = "match_parent">
</Spinner>
</LinearLayout> the layout of the Spinner Item borrowed the layout of the previous drawer navigation Item drawer_item.xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">
<ImageView
Android: id = "@ + id/drawer_img"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: id = "@ + id/drawer_title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">
<ImageView
Android: id = "@ + id/drawer_img"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: id = "@ + id/drawer_title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
The layout is ready. Write some information in the Activity.
First, prepare in the onCreate () method.
[Java]
/** Action_bar add custom view */
View actionbarLayout = LayoutInflater. from (this). inflate (R. layout. action_bar, null );
MActionbarSpinne = (Spinner) actionbarLayout. findViewById (R. id. action_bar_spinner );
// SetAdapter can be used based on the actual situation.
MActionbarSpinne. setAdapter (new SimpleAdapter (this,
GetDrawerItems (true), R. layout. drawer_item, new String [] {
"Drawer_img", "drawer_title"}, new int [] {
R. id. drawer_img, R. id. drawer_title }));
MActionbarSpinne. setOnItemSelectedListener (new SpinnerItemSelectedListener ());
// Enable the custom general View to be displayed in the title column, and actionBar. setCustomView can work.
ActionBar. setDisplayShowCustomEnabled (true );
ActionBar. setCustomView (actionbarLayout );
/** Action_bar add custom view */
View actionbarLayout = LayoutInflater. from (this). inflate (R. layout. action_bar, null );
MActionbarSpinne = (Spinner) actionbarLayout. findViewById (R. id. action_bar_spinner );
// SetAdapter can be used based on the actual situation.
MActionbarSpinne. setAdapter (new SimpleAdapter (this,
GetDrawerItems (true), R. layout. drawer_item, new String [] {
"Drawer_img", "drawer_title"}, new int [] {
R. id. drawer_img, R. id. drawer_title }));
MActionbarSpinne. setOnItemSelectedListener (new SpinnerItemSelectedListener ());
// Enable the custom general View to be displayed in the title column, and actionBar. setCustomView can work.
ActionBar. setDisplayShowCustomEnabled (true );
ActionBar. setCustomView (actionbarLayout); then
[Java]
/**
* Listens to the spinner item Click Event of action_bar.
*/
Private class SpinnerItemSelectedListener implements OnItemSelectedListener {
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View view, int position,
Long arg3 ){
SelectItem (view, position, MESSAGE_SPINNER_ITEM );
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){}
}
/**
* Listens to the spinner item Click Event of action_bar.
*/
Private class SpinnerItemSelectedListener implements OnItemSelectedListener {
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View view, int position,
Long arg3 ){
SelectItem (view, position, MESSAGE_SPINNER_ITEM );
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){}
} The selectItem () Details of Listener are as follows:
[Java]
If (messageId = MESSAGE_SPINNER_ITEM ){
If (position! = 0 ){
TextView title = (TextView) view. findViewById (R. id. drawer_title );
String cityName = title. getText (). toString ();
This. cityInput. setText (cityName );
}
}