Android Action Bar custom View

Source: Internet
Author: User

 

 


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 );
}
}

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.