Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the construction skills of Android XML?

Shulou Source: shulou.com Published: 2022-06-02 10:17:06 10月25日 Update

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.

Tags: Files methods menus folders more events questions techniques help answers easy same operating system easy personal people code advantages and disadvantages content function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Huawei OPPO Reno NVidia