In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the construction skills of Android XML? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In Android mobile operating system, there are many ways to achieve the same function. It depends on the programmer's personal preferences, the applicable environment, and so on. For example, the creation of Android menus can be implemented in a variety of ways, such as dynamic.
First, create a menu folder under res and a menu.xml under this folder
< ?xml version="1.0" encoding="utf-8"?> < menu xmlns:android="http://schemas.android.com/apk/res/android"> < item android:id="@+id/settings" android:title="@string/settings_label" android:alphabeticShortcut="@string/settings_shortcut" />/ / more items are added here
< /menu>Then add the following string resources to strings.xml under res/values:
< string name="settings_label">Settings...
< /string> < string name="settings_title">Sudoku settings
< /string> < string name="settings_shortcut">S
< /string> < string name="music_title">Music
< /string> < string name="music_summary">Play background music
< /string> < string name="hints_title">Hints
< /string> < string name="hints_summary">Show hints during play
< /string>Override the onCreateOptionsMenu event of the base class in the activity class
@ Override public boolean onCreateOptionsMenu (Menu menu) {super.onCreateOptionsMenu (menu); MenuInflater inflater = getMenuInflater (); inflater.inflate (R.menu.menu, menu); return true;}
In this way, an Android menu has been built. Note that the MenuInflater class is used here, and the inflate method of this class is used to read the xml file and create the menu. Note that there is only one item in the xml menu. If you need more items, you can continue to add them later.
Next, it's time to implement the events of each menu item. Override the onOptionsItemSelected method of the base class in the activity class:
@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item.getItemId ()) {case R.id.settings: startActivity (new Intent (this, Settings.class)); return true; / / More items go here (if any).} return false;}
In this event of the responsive Android menu, we create a new activity. The activity is rendered through the class Settings. We know that there are two ways to render an activity: (1) through the code layout and (2) through the xml file. Each approach has its advantages and disadvantages, and here we render the view through a xml file.
1) first, create a xml folder under the res file and a Settings.xml file under the xml folder.
< ?xml version="1.0" encoding="utf-8"?> < PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> < CheckBoxPreference android:key="music" android:title="@string/music_title" android:summary="@string/music_summary" android:defaultValue="true" /> < CheckBoxPreference android:key="hints" android:title="@string/hints_title" android:summary="@string/hints_summary" android:defaultValue="true" /> < /PreferenceScreen>2) establish the class Settings.java.
Package org.example.sudoku
Import android.os.Bundle
Import android.preference.PreferenceActivity
Public class Settings extends PreferenceActivity {
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
AddPreferencesFromResource (R.xml.settings)
}
}
This is the answer to the question about the construction skills of Android XML. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.