Android rounded corner background and pop-up popupwindow through the menu key function

Source: Internet
Author: User

Package com. Android. Menu;

Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. drawable. bitmapdrawable;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. gravity;
Import Android. View. layoutinflater;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. gridview;
Import Android. widget. imageview;
Import Android. widget. linearlayout;
Import Android. widget. linearlayout. layoutparams;
Import Android. widget. popupwindow;
Import Android. widget. textview;

Public class menutest extends activity {
Private string tag = This. getclass (). getsimplename ();

Private int [] resarray = new int [] {
R. drawable. Icon, R. drawable. Icon,
R. drawable. Icon, R. drawable. icon
};

Private string [] Title = new string [] {
"Add song", "Song information", "Search for lyrics", "Search for lyrics"
};

Private Static Boolean show_flag = false;

Private popupwindow PW = NULL;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}

@ Override
Public Boolean oncreateoptionsmenu (menu ){
Log. E (TAG, "------ oncreateoptionsmenu ------");

// Use alertdialog to pop up the menu
// View = layoutinflater. From (this). Inflate (R. layout. Menu, null );
// Gridview grid1 = (gridview) view. findviewbyid (R. Id. menugridchange );
// Grid1.setadapter (New imageadapter (this ));
// Builder build = new alertdialog. Builder (this );
// Build. setview (View );
// Build. Show ();

Layoutinflater Inflater = (layoutinflater) This. getsystemservice (context. layout_inflater_service );
View view = Inflater. Inflate (R. layout. Menu, null );
Gridview grid1 = (gridview) view. findviewbyid (R. Id. menugridchange );
Grid1.setadapter (New imageadapter (this ));

// Use popupwindow to pop up the menu
PW = new popupwindow (view, layoutparams. fill_parent, layoutparams. wrap_content );

// Nnd, the first parameter. You must find a view.
PW. showatlocation (findviewbyid (R. Id. TV), gravity. Center, 0,300 );

Return true;
}

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Return super. onoptionsitemselected (item );
}

Public class imageadapter extends baseadapter {

Private context;

Public imageadapter (context ){
This. Context = context;
}

@ Override
Public int getcount (){
Return resarray. length;
}

@ Override
Public object getitem (INT arg0 ){
Return resarray [arg0];
}

@ Override
Public long getitemid (INT arg0 ){
Return arg0;
}

@ Override
Public View getview (INT arg0, view arg1, viewgroup arg2 ){
Linearlayout Linear = new linearlayout (context );
Linearlayout. layoutparams Params = new layoutparams (layoutparams. wrap_content, layoutparams. wrap_content );
Linear. setorientation (linearlayout. Vertical );

Imageview IV = new imageview (context );
Iv. setimagebitmap (bitmapdrawable) Context. getresources (). getdrawable (resarray [arg0]). getbitmap ());
Linearlayout. layoutparams params2 = new layoutparams (layoutparams. wrap_content, layoutparams. wrap_content );
Params2.gravity = gravity. Center;
Linear. addview (IV, params2 );

Textview TV = new textview (context );
TV. settext (title [arg0]);
Linearlayout. layoutparams params3 = new layoutparams (layoutparams. wrap_content, layoutparams. wrap_content );
Params3.gravity = gravity. Center;

Linear. addview (TV, params3 );

Return linear;
}
}

}

2. sharder. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Shape Android: Shape = "rectangle"
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Solid Android: color = "# b4000000"/>
<Stroke Android: width = "2.0dip" Android: color = "# b4ffffff" Android: dashwidth = "3.0dip" Android: dashgap = "0.0dip"/>
<Padding Android: Left = "7.0dip" Android: Top = "7.0dip" Android: Right = "7.0dip" Android: Bottom = "7.0dip"/>
<Corners Android: radius = "8.0dip"/>
</Shape>

3. Menu. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Gridview Android: gravity = "center"
Android: layout_gravity = "center"
Android: Id = "@ + ID/menugridchange"
Android: Background = "@ drawable/sharder"
Android: padding = "5.0dip"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: horizontalspacing = "10.0dip"
Android: verticalspacing = "3.0dip"
Android: stretchmode = "columnwidth"
Android: columnwidth = "601_dip"
Android: numcolumns = "auto_fit"
Xmlns: Android = "http://schemas.android.com/apk/res/android"/>
</Linearlayout>

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.