Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize menu effect by Android studio

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to achieve the menu effect of Android studio, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. OnCreateOptionsMenu ()

First, create a new menu folder under the res directory, right-click the res directory → New → Directory, enter the folder name menu (optional), click OK, and then create a new main menu file under the menu folder. Click the menu folder new → menu resource file.

The activity.xml file code does not need to be changed.

MainActivity.java file code:

Package com.example.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) } / / use onCreateOptionsMenu () to display the menu @ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater () .menu.menu.menu / / return true, allow the created menu to be displayed, return false to do not show} / / define menu response event @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item.getItemId ()) {/ / item.getItemId () to judge which menu item we choose case R.id.first: Toast.makeText (this, "FIRST", Toast.LENGTH_SHORT). Show () Break; case R.id.second: Toast.makeText (this, SECOND, Toast.LENGTH_SHORT). Show (); break; default:} return true;}}

Key code: menu main.xml file code:

The three points in the upper right corner are the menu. click to open as shown in the figure:

The advance of one (the menu of the sub-menu)

Menu.xml:

.java:

Package com.example.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.widget.Toast;public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) } / / display menu @ Override public boolean onCreateOptionsMenu (Menu menu) {MenuInflater inflater=new MenuInflater (this) with onCreateOptionsMenu (); / / instantiate a MenuInflater object inflater.inflate (R.menu.menu. } / / define menu response event @ Override public boolean onOptionsItemSelected (MenuItem item) {if (item.getItemId () = = R.id.f1) Toast.makeText (this, "change background", Toast.LENGTH_SHORT) .show (); else if (item.getItemId () = = R.id.f3) Toast.makeText (this, "restore factory", Toast.LENGTH_SHORT) .show () Else {if (item.getGroupId () = = R.id.group1) {/ / determine whether the first layer second item is selected / / corresponding to the menu.xml file android:checkableBehavior= "all" if (item.isChecked ()) / / menu is selected item.setChecked (false) / / set menu items to be unchecked else item.setChecked (true); / / set menu items to be selected}} return true;}}

Effect:

II. OnCreateContextMenu () (context menu)

Main.xml:

Menu.xml:

.java:

Package com.example.myapplication;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.ContextMenu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) Tv= (TextView) findViewById (R.id.text1); registerForContextMenu (tv); / / register the context menu for text} / / display the menu @ Override public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {MenuInflater inflater=new MenuInflater (this) with onCreateContextMenu (); / / instantiate MenuInflater an object inflater.inflate (R.menu.menu.menu / / parse the menu file menu.setHeaderTitle ("Select Color:"); / / set the title} @ Override public boolean onContextItemSelected (MenuItem item) {switch (item.getItemId ()) {case R.id.red: tv.setTextColor (Color.rgb (255)) for the menu header; break Case R.id.green: tv.setTextColor (Color.rgb (0555)); break; default: tv.setTextColor (Color.rgb (0Power0255);} return true;}}

Long press to open the menu.

Choose any color and open the menu and the four words will change to the corresponding color.

After reading the above, do you have any further understanding of how Android studio achieves menu effects? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report