Ⅰ 我想在android中設置兩個下拉菜單
android 下拉菜單
1.<?xml version="1.0" encoding="utf-8"?>
12.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
13. android:orientation="vertical" android:gravity="right"
14. android:layout_width="fill_parent" android:layout_height="fill_parent">
15.
19. <FrameLayout android:layout_height="wrap_content"
20. android:layout_width="fill_parent">
21. <TextView android:layout_width="wrap_content"
22. android:layout_height="wrap_content" android:text="FrameLayout">
23. </TextView>
24. <TextView android:layout_width="wrap_content"
25. android:layout_height="wrap_content" android:text="Frame Layout">
26. </TextView>
27. </FrameLayout>
28.
29. <TextView android:layout_width="wrap_content"
30. android:layout_height="wrap_content" android:text="@string/hello" />
31.
39. <TableLayout android:id="@+id/TableLayout01"
40. android:layout_width="fill_parent" android:layout_height="wrap_content"
41. android:collapseColumns="1">
42. <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
43. android:layout_height="wrap_content">
44. <TextView android:layout_width="wrap_content"
45. android:layout_weight="1" android:layout_height="wrap_content"
46. android:text="行1列1" />
47. <TextView android:layout_width="wrap_content"
48. android:layout_weight="1" android:layout_height="wrap_content"
49. android:text="行1列2" />
50. <TextView android:layout_width="wrap_content"
51. android:layout_weight="1" android:layout_height="wrap_content"
52. android:text="行1列3" />
53. </TableRow>
54. <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
55. android:layout_height="wrap_content">
56. <TextView android:layout_width="wrap_content"
57. android:layout_height="wrap_content" android:text="行2列1" />
58. </TableRow>
59. </TableLayout>
60.
66. <AbsoluteLayout android:layout_height="wrap_content"
67. android:layout_width="fill_parent">
68. <TextView android:layout_width="wrap_content"
69. android:layout_height="wrap_content" android:text="AbsoluteLayout"
70. android:layout_x="100px"
71. android:layout_y="100px" />
72. </AbsoluteLayout>
73.
81. <RelativeLayout android:id="@+id/RelativeLayout01"
82. android:layout_width="fill_parent" android:layout_height="fill_parent">
83. <TextView android:layout_width="wrap_content" android:id="@+id/abc"
84. android:layout_height="wrap_content" android:text="centerInParent=true"
85. android:layout_centerInParent="true" />
86. <TextView android:layout_width="wrap_content"
87. android:layout_height="wrap_content" android:text="marginLeft=20px"
88. android:layout_marginLeft="20px" />
89. <TextView android:layout_width="wrap_content"
90. android:layout_height="wrap_content" android:text="xxx"
91. android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
92. </RelativeLayout>
93.
94.</LinearLayout>
95.
96.
97.res/values/strings.xml
98.<?xml version="1.0" encoding="utf-8"?>
99.<resources>
100. <string name="hello">Hello Layout</string>
101. <string name="app_name">webabcd_layout</string>
102.</resources>
103.
104.
105.Main.java
106.
107.代碼
108.package com.webabcd.layout;
109.
110.import android.app.Activity;
111.import android.os.Bundle;
112.
113.public class Main extends Activity {
114. /** Called when the activity is first created. */
115. @Override
116. public void onCreate(Bundle savedInstanceState) {
117. super.onCreate(savedInstanceState);
118. setContentView(R.layout.main);
119. }
120.}
121.
122.
123.2、上下文菜單,選項菜單,子菜單
124.res/layout/main.xml
125.
126.代碼
127.<?xml version="1.0" encoding="utf-8"?>
128.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
129. android:orientation="vertical" android:layout_width="fill_parent"
130. android:layout_height="fill_parent">
131.
132. <TextView android:id="@+id/txt1" android:layout_width="fill_parent"
133. android:layout_height="wrap_content" android:text="@string/hello_contextMenu" />
134.
135. <TextView android:id="@+id/txt2" android:layout_width="fill_parent"
136. android:layout_height="wrap_content" android:text="@string/hello_subMenu" />
137.
138.</LinearLayout>
139.
140.
141.res/values/strings.xml
142.
143.代碼
144.<?xml version="1.0" encoding="utf-8"?>
145.<resources>
146. <string name="hello_contextMenu">Hello Context Menu</string>
147. <string name="hello_subMenu">Hello Context Sub Menu</string>
148. <string name="app_name">webabcd_menu</string>
149.</resources>
150.
151.
152.Main.java
153.
154.代碼
155.package com.webabcd.menu;
156.
157.import android.app.Activity;
158.import android.os.Bundle;
159.import android.view.ContextMenu;
160.import android.view.Menu;
161.import android.view.MenuItem;
162.import android.view.SubMenu;
163.import android.view.View;
164.import android.view.ContextMenu.ContextMenuInfo;
165.import android.widget.TextView;
166.import android.widget.Toast;
167.
168.// 演示兩種菜單的實現方式:上下文菜單(通過在某元素上長按,來呼出菜單)和選項菜單(通過按手機上的菜單按鈕,來呼出菜單)
169.public class Main extends Activity {
170. /** Called when the activity is first created. */
171. @Override
172. public void onCreate(Bundle savedInstanceState) {
173. super.onCreate(savedInstanceState);
174. setContentView(R.layout.main);
175.
176. // 為 R.id.txt1 注冊一個上下文菜單(在此 TextView 上長按,則會呼出上下文菜單)
177. // 具體呼出的菜單內容需要重寫 onCreateContextMenu 來創建
178. TextView txt1 = (TextView) this.findViewById(R.id.txt1);
179. this.registerForContextMenu(txt1);
180.
181. // 為 R.id.txt2 注冊一個上下文菜單
182. TextView txt2 = (TextView) this.findViewById(R.id.txt2);
183. this.registerForContextMenu(txt2);
184. }
185.
186. // 重寫 onCreateContextMenu 用以創建上下文菜單
187. // 重寫 onContextItemSelected 用以響應上下文菜單
188. @Override
189. public void onCreateContextMenu(ContextMenu menu, View v,
190. ContextMenuInfo menuInfo) {
191. super.onCreateContextMenu(menu, v, menuInfo);
192.
193. // 創建 R.id.txt1 的上下文菜單
194. if (v == (TextView) this.findViewById(R.id.txt1)) {
195.
196. // ContextMenu.setIcon() - 設置菜單的圖標
197. // ContextMenu.setHeaderTitle() - 設置菜單的標題
198. menu.setHeaderIcon(R.drawable.icon01);
199. menu.setHeaderTitle("我是菜單");
200.
201. // 用 ContextMenu.add() 來增加菜單項,返回值為 MenuItem
202. // 第一個參數:組ID
203. // 第二個參數:菜單項ID
204. // 第三個參數:順序號
205. // 第四個參數:菜單項上顯示的內容
206. menu.add(1, 0, 0, "菜單1");
207.
208. // MenuItem - 新增菜單項後的返回類型,針對菜單項的其他設置在此對象上操作
209. menu.add(1, 1, 1, "菜單2").setCheckable(true);
210.
211. }
212. // 創建 R.id.txt2 的上下文菜單(多級上下文菜單)
213. else if (v == (TextView) this.findViewById(R.id.txt2)) {
214.
215. // ContextMenu.addSubMenu("菜單名稱") - 用來添加子菜單。子菜單其實就是一個特殊的菜單
216. SubMenu sub = menu.addSubMenu("父菜單1");
217. sub.setIcon(R.drawable.icon01);
218. sub.add(0, 0, 0, "菜單1");
219. sub.add(0, 1, 1, "菜單2");
220. sub.setGroupCheckable(1, true, true);
221.
222. SubMenu sub2 = menu.addSubMenu("父菜單2");
223. sub2.setIcon(R.drawable.icon01);
224. sub2.add(1, 0, 0, "菜單3");
225. sub2.add(1, 1, 1, "菜單4");
226. sub2.setGroupCheckable(1, true, false);
227.
228. }
229. }
230.
231.
232. // 重寫 onCreateOptionsMenu 用以創建選項菜單
233. @Override
234. public boolean onCreateOptionsMenu(Menu menu) {
235.
236. MenuItem menuItem = menu.add(0, 0, 0, "菜單111111111111111111111");
237.
238. // MenuItem.setIcon() - 設置菜單項的圖標
239. // MenuItem.setTitleCondensed() - 菜單的簡標題,如果指定了簡標題的話,菜單項上的標題將會以此簡標題為准
240. // MenuItem.setAlphabeticShortcut() - 設置選中此菜單項的快捷鍵
241. // 註:菜單項超過 6 個的話,第 6 個菜單將會變為 More 菜單,多餘的菜單會在單擊 More 菜單之後顯示出來
242. menuItem.setIcon(R.drawable.icon01);
243. menuItem.setTitleCondensed("菜單1");
244. menuItem.setAlphabeticShortcut('a');
245.
246. menu.add(0, 1, 1, "菜單2").setIcon(R.drawable.icon02);
247. menu.add(0, 2, 2, "菜單3").setIcon(R.drawable.icon03);
248. menu.add(0, 3, 3, "菜單4");
249. menu.add(0, 4, 4, "菜單5");
250. menu.add(0, 5, 5, "菜單6");
251. menu.add(0, 6, 6, "菜單7").setIcon(R.drawable.icon04);
252. menu.add(0, 7, 7, "菜單8").setIcon(R.drawable.icon05);
253.
254. return true;
255. }
256.
257. // 重寫 onOptionsItemSelected 用以響應選項菜單
258. @Override
259. public boolean onOptionsItemSelected(MenuItem item) {
260. super.onOptionsItemSelected(item);
261.
262. Toast.makeText(Main.this, "被單擊的菜單項為:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();
263.
264. return false;
265. }
266.}
Ⅱ android 進度條樣式 怎麼改
Android系統提供了兩大類進度條樣式,長形進度條(progressBarStyleHorizontal) 和圓形進度條(progressBarStyleLarge)。
android 進度條樣式更改:
第一種
(默認樣式(中等圓形))
進度條用處很多,比如,應用程序裝載資源和網路連接時,可以提示用戶稍等,這一類進度條只是代表應用程序中某一部分的執行情況,而整個應用程序執行情況呢,則可以通過應用程序標題欄來顯示一個進度條,這就需要先對窗口的顯示風格進行設置"requestWindowFeature(Window.FEATURE_PROGRESS)"。
Ⅲ Android 模擬器中按菜單Menu鍵彈出菜單項MenuItem,點擊菜單項中的「退出」沒反應
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*
*
* add()方法的四個參數,依次是:
*
* 1、組別,如果不分組的話就寫Menu.NONE,
*
* 2、Id,這個很重要,Android根據這個Id來確定不同的菜單
*
* 3、順序,那個菜單現在在前面由這個參數的大小決定
*
* 4、文本,菜單的顯示文本
*/
//圖標文件實現android系統自帶的文件
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "保存").setIcon(android.R.drawable.ic_menu_save);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "添加").setIcon(android.R.drawable.ic_menu_add);
// menu.add(Menu.NONE, Menu.FIRST + 3, 3, "刪除").setIcon(android.R.drawable.ic_menu_delete);
// menu.add(Menu.NONE, Menu.FIRST + 4, 4, "發送").setIcon(android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, Menu.FIRST + 5, 5, "幫助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 6, 6, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST + 1:
Toast.makeText(WqtMainActivity.this, "保存菜單被點擊了!", Toast.LENGTH_LONG).show();
//intent = new Intent(MenuActivity.this,Myhandler.class);
//startActivity(intent);
//overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;
case Menu.FIRST + 2:
Toast.makeText(WqtMainActivity.this, "添加菜單被點擊了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(WqtMainActivity.this, "刪除菜單被點擊了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(WqtMainActivity.this, "發送菜單被點擊了!", Toast.LENGTH_LONG).show();
//通知在狀態欄顯示的圖標
//notification.icon = android.R.drawable.ic_lock_silent_mode_off;
//通知的內容
//notification.tickerText = "發送菜單被點擊了!";
//通知時發出的聲音
//notification.defaults = Notification.DEFAULT_SOUND;
//intent = new Intent(MenuActivity.this,mNotification.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//pendindIntent = PendingIntent.getActivity(MenuActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//notification.setLatestEventInfo(MenuActivity.this, "按鈕", "發送按鈕", pendindIntent);
//notificationManager.notify(913, notification);
break;
case Menu.FIRST + 5:
Toast.makeText(WqtMainActivity.this, "幫助菜單被點擊了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
AlertDialog alertDialog = new AlertDialog.Builder(WqtMainActivity.this)
.setTitle("提示!")
.setIcon(R.drawable.ask)
.setMessage("您確定要退出系統嗎?")
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
dialog.cancel(); //提示對話框關閉
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel(); //關閉對話框
}
}).create();
alertDialog.show();
break;
}
return super.onMenuItemSelected(featureId, item);
}
Ⅳ android開發怎麼弄成,點擊圖標後彈出一個消息框。主界面不顯示
這里報錯是代碼寫法有誤,建議一行一行代碼的寫。
具體實現代碼:
1.創建對象框
AlertDialog.Builder builder = new Builder(context);
builder.setTitle("請輸入"); //設置對話框標題
builder.setIcon(android.R.drawable.btn_star); //設置對話框標題前的圖標
2.創建EditText輸入框
final EditText edit = new EditText(context);
3.將輸入框賦值給Dialog,並增加確定取消按鍵
builder.setView(edit);
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "你輸入的是: " + edit.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "你點了取消", Toast.LENGTH_SHORT).show();
}
});
4.設置常用api,並show彈出
builder.setCancelable(true); //設置按鈕是否可以按返回鍵取消,false則不可以取消
AlertDialog dialog = builder.create(); //創建對話框
dialog.setCanceledOnTouchOutside(true); //設置彈出框失去焦點是否隱藏,即點擊屏蔽其它地方是否隱藏
dialog.show();
Ⅳ Android中menu菜單中的圖片是怎麼加進去的
給這個圖片按鈕添加一個事件, button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(當前Activity.this, 下一個Activity.class);
startActivity(intent);
當前Activity.finish();//關閉當前Activity
}
});
與普通按鈕沒有什麼區別,一個就是有圖片的,一個沒有嘛,對它做事件都 一樣,只不過顯示效果不一樣。
Ⅵ 如何獲取android 進程信息
獲取Android系統的可用內存和總內存是開發過程中常見的需求。通常,開發者會使用ActivityManager類來獲取這些信息,然而這可能會導致數據不準確。一種更可靠的方法是讀取//proc/meminfo文件,該文件詳細記錄了Android設備的各種信息,包括可用內存和總內存。
下面提供了一個示例代碼來獲取總內存大小:
public static long getTotalMemSize() { long size=0; File file = new File("/proc/meminfo"); try { BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String memInfo = buffer.readLine(); int startIndex = memInfo.indexOf(":"); int endIndex = memInfo.indexOf("k"); memInfo = memInfo.substring(startIndex + 1, endIndex).trim(); size = Long.parseLong(memInfo); size *= 1024; buffer.close(); } catch (java.io.IOException e) { e.printStackTrace(); } return size; }
此代碼通過讀取第一行數據並進行字元串處理來計算總內存大小。獲取可用內存大小的方法類似,只是讀取第二行數據。
接下來,獲取內存中運行的應用信息。首先創建一個Bean文件用於存儲這些信息。然後,通過ActivityManager的getRunningAppProcesses()方法獲取一個RunningAppProcessInfo的List。遍歷這個List,提取所需數據並存儲在Bean文件中。
public static List getAllTask() { List taskList=new ArrayList<>(); List runList=UIUtils.getActManager().getRunningAppProcesses(); try { for (ActivityManager.RunningAppProcessInfo r:runList) { TaskBean taskBean = new TaskBean(); String processName = r.processName; taskBean.setPackageName(processName); PackageInfo packageInfo = UIUtils.getPacManager().getPackageInfo(processName, 0); taskBean.setIcon(packageInfo.applicationInfo.loadIcon(UIUtils.getPacManager())); taskBean.setName(packageInfo.applicationInfo.loadLabel(UIUtils.getPacManager()).toString()); Debug.MemoryInfo[] processInfo=UIUtils.getActManager().getProcessMemoryInfo(new int[]{r.pid}); taskBean.setMemSize(processInfo[0].getTotalPrivateDirty()*1024); if ((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0){ taskBean.setSystem(true); }else { taskBean.setUser(true); } if (taskList != null) { taskList.add(taskBean); for (int i=0;i if (taskList.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){ taskList.remove(i); } } } } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return taskList; }
上述代碼遍歷運行的程序並獲取其中的信息。然後,根據應用的包名獲取內存信息,設置內存大小,並通過PackageManager獲取應用程序的信息。根據應用的標志判斷是系統進程還是用戶進程。
需要注意的是,從Android 5.0開始,谷歌棄用了通過上述方法獲取進程信息。為此,可以使用新的方法獲取進程信息。
public static List getTaskInfos() { List processInfos = ProcessManager.getRunningAppProcesses(); List taskinfos = new ArrayList(); // 遍歷運行的程序,並且獲取其中的信息 for (AndroidAppProcess processInfo : processInfos) { TaskBean taskinfo = new TaskBean(); // 應用程序的包名 String packname = processInfo.name; taskinfo.setPackageName(packname); // 湖區應用程序的內存 信息 android.os.Debug.MemoryInfo[] memoryInfos = UIUtils.getActManager() .getProcessMemoryInfo(new int[] { processInfo.pid }); long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024L; taskinfo.setMemSize(memsize); taskinfo.setPackageName(processInfo.getPackageName()); try { // 獲取應用程序信息 ApplicationInfo applicationInfo = UIUtils.getPacManager().getApplicationInfo( packname, 0); Drawable icon = applicationInfo.loadIcon(UIUtils.getPacManager()); taskinfo.setIcon(icon); String name = applicationInfo.loadLabel(UIUtils.getPacManager()).toString(); taskinfo.setName(name); if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { // 用戶進程 taskinfo.setUser(true); } else { // 系統進程 taskinfo.setSystem(true); } } catch (PackageManager.NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); // 系統內核進程 沒有名稱 taskinfo.setName(packname); Drawable icon = UIUtils.getContext().getResources().getDrawable( R.drawable.ic_launcher); taskinfo.setIcon(icon); } if (taskinfo != null) { taskinfos.add(taskinfo); for (int i=0;i if (taskinfos.get(i).getPackageName().equals(Constants.PACKAGE_INFO)){ taskinfos.remove(i); } } } } return taskinfos; }
此代碼通過獲取運行的程序信息並設置內存大小來獲取應用信息。根據應用的標志判斷是系統進程還是用戶進程。
最後,通過判斷Android系統的版本來選擇合適的方法獲取進程信息。如果系統版本大於21,則使用新的方法獲取進程信息;否則,使用舊的方法。
Ⅶ android中的menu的背景顏色如何設置請指教
.Factory,但是細節不是太清楚,請指點。 在strings.xml里配下info的顏色,如果是代碼生成的可以用這個menu.add(0, 1, 0, "確定").setIcon(R.drawable.info);不知道這樣是否符合你的要求drawable是圖片的目錄,strings是一些文本的屬性,你說的能具體點嗎? 問題補充:sunquanfeng 寫道引用sunquanfeng 寫道 在strings.xml里配下info的顏色,如果是代碼生成的可以用這個menu.add(0, 1, 0, "確定").setIcon(R.drawable.info);不知道這樣是否符合你的要求 android工程里不是有個strings.xml文件么,裡面是配置資源的,你配一個 <drawable name="info">#0000ff</drawable>,我說的不是目錄,你配好了就可以引用啊您說的方法不能解決問題。
Ⅷ android如何創建帶3個按鈕的對話框
1.先在布局界面上,拖進來一個按鈕控制項,並設置顯示的文字,記得保存(Ctrl+S)
之後在代碼界面上定義該按鈕。
2.新建一個按鈕點擊的方法。
onClick(View v) :點擊之後的動作。
3.設置按鈕的點擊事件指向我們新建的點擊方法。
setOnClickListener:設置點擊之後觸發的動作。
4.在onClick里添加彈出對話框的代碼。
AlertDialog:一個對話框類。
MainActivity.this:對話框顯示的位置。
setTitle:設置標題。
setMessage:設置內容。
setPositiveButton:設置對話框的按鈕。
show():顯示對話框。
至此所有代碼已經完成,編譯並生成,在Android安卓虛擬機上顯示如下。