Quiero configurar dos menús desplegables en Android
menú desplegable de Android
1.lt;?xml version="1.0" encoding="utf-8"?gt
12.lt; ; LinearLayout xmlns: android="/apk/res/android"
13. android:orientación="vertical" android:gravity="right"
14. "fill_parent" android: layout_height="fill_parent"gt;
15
19. : diseño_ancho ="fill_parent"gt;
21. TextView android: diseño_width="wrap_content"
22. gt;
23./TextViewgt;
24.TextView android: diseño_width="wrap_content"
25. " android : text="Frame Layout"gt;
26. lt;/TextViewgt;
27. lt;/FrameLayoutgt;
28. p>
29. lt; TextView android: layout_width="wrap_content"
30. android: layout_height="wrap_content" android: text="@string/hello" /gt;
31.
39. lt; TableLayout android: id="@ id/TableLayout01"
40. p>
41. android: colapsoColumns="1"gt;
42. lt; >
43.Android:layout_height="w
rap_content"gt;
44. lt; TextView android:layout_width="wrap_content"
45. android:layout_weight="1" android:layout_height="wrap_content"
46. android: text="fila 1 columna 1" /gt;
47. "1" android: layout_height="wrap_content"
49. android: text="fila 1 columna 2" /gt; lt; "
51. android: layout_weight="1" android: layout_height="wrap_content"
52. android: text="fila 1 columna 3" /gt;
53./TableRowgt;
54.lt;TableRow android: id="@ id/TableRow01" android: layout_width="wrap_content"
55. layout_height="wrap_content"gt;
56. lt; TextView android: layout_width="wrap_content"
57. 1" /gt;
58. lt; /TableRowgt;
59. lt; /TableLayoutgt;
60.
66 lt; AbsoluteLayout android: layout_height="wrap_content"
67. android: layout_width="fill_parent"gt
68. p>
p>
69. android:layout_height="wrap_content" android:text="AbsoluteLayout"
70.
android: diseño_x="100px"
71. android: diseño_y="100px" /gt; lt /AbsoluteLayoutgt; /p>
81. lt; RelativeLayout android: id="@ id/RelativeLayout01"
82.
83. lt; TextView android: layout_width="wrap_content" android: id="@ id/abc"
84.
85. android: layout_centerInParent="true" /gt;
86. ="wrap_content" android: text="marginLeft=20px"
88. android: layout_marginLeft="20px" /gt;
89. "
90. android:layout_height="wrap_content" android:text="xxx"
91. android:layout_below="@id/abc" android:layout_alignRight="@id /abc" /gt;
92.lt;/RelativeLayoutgt;
93.
94.lt;/LinearLayoutgt;
95.
96.
97.res/values/strings.xml
98.lt;?xml versión="1.0" codificación="utf- 8"?gt;
99.lt; resourcesgt;
100.lt; string nombre="hola"gt; Hola diseñolt;/stringgt;
101.lt;string name="app_name"gt;webabcd_layoutlt;/stringgt
102.lt;/resour;
cesgt;
103
104
105.Main.java
107. Código
108.package com.webabcd.layout;
109
110.import android.app.Activity
111. .import android.os.Bundle;
112.
113.public class Main extiende la actividad {
114. creado */
115. @Override
116. public void onCreate(Bundle saveInstanceState) {
117. p> p>
118. setContentView(R.layout.main);
119. }
120.}
121. >
122.
123.2. Menú contextual, menú de opciones, submenú
124.res/layout/main.xml
125. >
126. Código
127.lt;?xml version="1.0" encoding="utf-8"?gt
128.lt; LinearLayout xmlns; = "/apk/res/android"
129. android: orientación="vertical" android: layout_width="fill_parent"
130.
131.
132. TextView android: id="@ id/txt1" android: layout_width="fill_parent"
133. = "wrap_content" android: text="@string/hello_contextMenu" /gt;
134.
lt; TextView android: id="@ id/txt2" layout_width ="fill_parent"
136. android:layout_height="wrap_content" android:text="@string/hello_subMenu"
137.
138.lt;/LinearLayoutgt;
139
140
141.res/values/strings.xml. p>
p>
142.
143. Código
144.lt;?xml version="1.0" codificación="utf-8"?gt;
145.lt; recursosgt;
146. lt; string name="hello_contextMenu"gt; Hola menú contextual;
147. string name="hello_subMenu" "gt; Hola Contexto Sub Menult;/stringgt;
148. lt; string name="app_name"gt; webabcd_menult;/stringgt;
149. lt;/recursosgt;
150
151
152.Main.java
153. >154.Código
p>
155.paquete com.webabcd.menu;
156
157.importar android.app.Actividad;
158.importar android.os.Bundle
159.importar android.view.ContextMenu
160.importar android.view.Menu
p>
161.importar android.view .MenuItem;
162.importar android.view.SubMenu
163.importar android.view.View
;164.importar android.view.ContextMenu .ContextMenuInfo;
165.importar android.widget.TextView
166.importar android.widget.Toast
;167.
168.// Demuestra la implementación de dos menús: menú contextual (llama el menú presionando prolongadamente un elemento) y menú de opciones (llama el menú presionando el botón de menú). en el teléfono)
169.public class Main extiende la actividad {
170 /** Se llama cuando se crea la actividad por primera vez */
171. @Override
172 . public void onCreate(Bundle saveInstanceState) {
173.
te);
174. setContentView(R.layout.main);
175.
176. Menú (al presionar prolongadamente este TextView aparecerá el menú contextual)
177. // El contenido específico del menú debe crearse sobrescribiendo onCreateContextMenu
178. ( TextView) this.findViewById(R.id.txt1);
179.registerForContextMenu(txt1);
181. es R.id.txt2 registra un menú contextual
182. TextView txt2 = (TextView) this.findViewById(R.id.txt2
183. ) ;
184. }
185.
186. // Anula onCreateContextMenu para crear un menú contextual. Anular onContextItemSelected para responder al menú contextual
188. @Override
189. public void onCreateContextMenu(menú ContextMenu, Ver v,
190. ContextMenuInfo menuInfo) {
191. super.onCreateContextMenu(menu, v, menuInfo);
192.
193. txt1
194.if (v == (TextView) this.findViewById(R.id.txt1)) {
195. ContextMenu .setIcon() - Establece el icono del menú
197. // ContextMenu.setHeaderTitle() - Establece el título del menú
198. drawable.icon01);
199. menu.setHeaderTitle("Soy un menú");
201. () para agregar elementos de menú, el valor de retorno es MenuItem
20
2. // El primer parámetro: ID de grupo
203. // El segundo parámetro: ID de elemento de menú
204 // El tercer parámetro: número de secuencia
p. >205. // El cuarto parámetro: el contenido que se muestra en el elemento del menú
206.add(1, 0, 0, "Menú 1"); >207.
208. // MenuItem: el tipo de retorno después de agregar un nuevo elemento de menú, opera en este objeto para otras configuraciones del elemento de menú
209. 1, 1, 1, "Menú 2").setCheckable(true);
210
211. Menú contextual de id.txt2 (menú contextual de varios niveles)
213. else if (v == (TextView) this.findViewById(R.id.txt2)) {
214
215. // ContextMenu.addSubMenu("nombre del menú") - usado para agregar submenú.
El submenú es en realidad un menú especial
216. SubMenu sub = menu.addSubMenu("Menú principal 1");
218.sub.add(0, 0, 0, "Menú 1");
219.sub.add(0, 1, 1, "Menú 2");
220. sub.setGroupCheckable(1, verdadero, verdadero);
221.
222.
223. sub2.setIcon(R.drawable.icon01);
224.sub2.add(1, 0, 0, "Menú 3"); >
p>
225. sub2.add(1, 1, 1, "Menú 4");
226.setGroupCheckable(1, verdadero, falso
227.
228.
229.
231. >232. // Anular onCreateOptionsMenu para crear un menú de opciones
233. @Override
234. onCreateOptionsMenu público (menú de menú) {
235. /p>
236. MenuItem menuItem = menu.add(0, 0, 0, "Menu11111111111111111111");
237
238. ) - Establece el icono del elemento del menú
239. // MenuItem.setTitleCondensed() - El título breve del menú Si se especifica un título breve, el título del elemento del menú se basará en. este breve título
240. // MenuItem.setAlphabeticShortcut() - Establece la tecla de acceso directo para seleccionar este elemento del menú
241 // Nota: si hay más de 6 elementos del menú. , el sexto menú se convertirá en el menú Más, el menú adicional se mostrará después de hacer clic en el menú Más
242. .setTitleCondensed("Menú 1");
244.
menuItem.setAlphabeticShortcut('a');
245
246.
247. menu.add(0, 2, 2, "Menú 3").setIcon(R.drawable.icon03); , 3, 3, "Menú 4");
249.menú.add(0, 4, 4, "Menú 5"); , 5, 5, "Menú 6");
251. menu.add(0, 6, 6, "Menú 7").setIcon(R.drawable.icon04); p>252.menú.add(0, 7, 7, "Menú 8").setIcon(R.drawable.icon05);
253.
255. }
256.
257. // Anular onOptionsItemSelected para responder al menú de opciones
258. /p>
259. public boolean onOptionsItemSelected(MenuItem item) {
260.onOptionsItemSelected(item);
261. Toast.makeText(Main.this, "El elemento del menú en el que se hizo clic es:" String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show()
263. p>264. devuelve falso;
265.
266.}