1. android怎麼實現左側固定菜單,右側顯示不同布局的效果
viewpager+fragment
2. android 怎樣用HorizontalScrollView左右彈性的菜單
{
SlidingMenumSlidingMenu;
@Override
protectedvoidonCreate(Bundlearg0){
super.onCreate(arg0);
setContentView(R.layout.main);
DisplayMetricsdm=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mSlidingMenu=(SlidingMenu)findViewById(R.id.slidingMenu);
mSlidingMenu.setAlignScreenWidth((dm.widthPixels/5)*2);
ViewleftView=getLayoutInflater().inflate(R.layout.left_menu,null);
ViewrightView=getLayoutInflater().inflate(R.layout.right_menu,null);
ViewcenterView=getLayoutInflater().inflate(R.layout.center,null);
mSlidingMenu.setLeftView(leftView);
mSlidingMenu.setRightView(rightView);
mSlidingMenu.setCenterView(centerView);
ButtonshowLeftMenu=(Button)centerView.findViewById(R.id.center_left_btn);
showLeftMenu.setOnClickListener(this);
ButtonshowRightMenu=(Button)centerView.findViewById(R.id.center_right_btn);
showRightMenu.setOnClickListener(this);
}
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
switch(v.getId()){
caseR.id.center_left_btn:
mSlidingMenu.showLeftView();
break;
caseR.id.center_right_btn:
mSlidingMenu.showRightView();
break;
default:
break;
}
}
}
3. android 怎麼實現點擊菜單在頂部和底部同時呼出不同的菜單選項
菜單是用戶界面中最常見的元素之一,使用非常頻繁,在Android中,菜單被分為如下三種,選項菜單(OptionsMenu)、上下文菜單(ContextMenu)和子菜單(SubMenu),以下說的是創建OptionsMenu
一、概述
public boolean onCreateOptionsMenu(Menu menu):使用此方法調用OptionsMenu。
public boolean onOptionsItemSelected(MenuItem item):選中菜單項後發生的動作。
public void onOptionsMenuClosed(Menu menu):菜單關閉後發生的動作。
public boolean onPrepareOptionsMenu(Menu menu):選項菜單顯示之前onPrepareOptionsMenu方法會被調用,你可以用此方法來根據打當時的情況調整菜單。
public boolean onMenuOpened(int featureId, Menu menu):單打開後發生的動作。
二、默認樣式
默認樣式是在屏幕底部彈出一個菜單,這個菜單我們就叫他選項菜單OptionsMenu,一般情況下,選項菜單最多顯示2排每排3個菜單項,這些菜單項有文字有圖標,也被稱作Icon Menus,如果多於6項,從第六項開始會被隱藏,在第六項會出現一個More里,點擊More才出現第六項以及以後的菜單項,這些菜單項也被稱作Expanded Menus。下面介紹。
1.main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="請點擊Menu鍵顯示選項菜單"
android:id="@+id/TextView02"/>
</LinearLayout>
2。重載onCreateOptionsMenu(Menu menu)方法
重載onCreateOptionsMenu(Menu menu)方法,並在此方法中添加菜單項,最後返回true,如果false,菜單則不會顯示。
(Menumenu)
@Override
(Menumenu){
/*
*
*add()方法的四個參數,依次是:
*
*1、組別,如果不分組的話就寫Menu.NONE,
*
*2、Id,這個很重要,Android根據這個Id來確定不同的菜單
*
*3、順序,那個菜單現在在前面由這個參數的大小決定
*
*4、文本,菜單的顯示文本
*/
menu.add(Menu.NONE,Menu.FIRST+1,5,"刪除").setIcon(
android.R.drawable.ic_menu_delete);
//setIcon()方法為菜單設置圖標,這里使用的是系統自帶的圖標,同學們留意一下,以
//android.R開頭的資源是系統提供的,我們自己提供的資源是以R開頭的
menu.add(Menu.NONE,Menu.FIRST+2,2,"保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,Menu.FIRST+3,6,"幫助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE,Menu.FIRST+4,1,"添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+5,4,"詳細").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE,Menu.FIRST+6,3,"發送").setIcon(
android.R.drawable.ic_menu_send);
returntrue;
}
3。為菜單項注冊事件
使用onOptionsItemSelected(MenuItem item)方法為菜單項注冊事件
(MenuItemitem)
@Override
(MenuItemitem){
switch(item.getItemId()){
caseMenu.FIRST+1:
Toast.makeText(this,"刪除菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+2:
Toast.makeText(this,"保存菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+3:
Toast.makeText(this,"幫助菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+4:
Toast.makeText(this,"添加菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+5:
Toast.makeText(this,"詳細菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+6:
Toast.makeText(this,"發送菜單被點擊了",Toast.LENGTH_LONG).show();
break;
}
returnfalse;
}
4.完整代碼
packagecom.android.menu;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.Toast;
{
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
(Menumenu){
/*
*
*add()方法的四個參數,依次是:
*
*1、組別,如果不分組的話就寫Menu.NONE,
*
*2、Id,這個很重要,Android根據這個Id來確定不同的菜單
*
*3、順序,那個菜單現在在前面由這個參數的大小決定
*
*4、文本,菜單的顯示文本
*/
menu.add(Menu.NONE,Menu.FIRST+1,5,"刪除").setIcon(
android.R.drawable.ic_menu_delete);
//setIcon()方法為菜單設置圖標,這里使用的是系統自帶的圖標,同學們留意一下,以
//android.R開頭的資源是系統提供的,我們自己提供的資源是以R開頭的
menu.add(Menu.NONE,Menu.FIRST+2,2,"保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,Menu.FIRST+3,6,"幫助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE,Menu.FIRST+4,1,"添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+5,4,"詳細").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE,Menu.FIRST+6,3,"發送").setIcon(
android.R.drawable.ic_menu_send);
returntrue;
}
@Override
(MenuItemitem){
switch(item.getItemId()){
caseMenu.FIRST+1:
Toast.makeText(this,"刪除菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+2:
Toast.makeText(this,"保存菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+3:
Toast.makeText(this,"幫助菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+4:
Toast.makeText(this,"添加菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+5:
Toast.makeText(this,"詳細菜單被點擊了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+6:
Toast.makeText(this,"發送菜單被點擊了",Toast.LENGTH_LONG).show();
break;
}
returnfalse;
}
@Override
publicvoidonOptionsMenuClosed(Menumenu){
Toast.makeText(this,"選項菜單關閉了",Toast.LENGTH_LONG).show();
}
@Override
(Menumenu){
Toast.makeText(this,
"選項菜單顯示之前onPrepareOptionsMenu方法會被調用,你可以用此方法來根據打當時的情況調整菜單",
Toast.LENGTH_LONG).show();
//如果返回false,此方法就把用戶點擊menu的動作給消費了,onCreateOptionsMenu方法將不會被調用
returntrue;
}
}
5.運行效果
4. android 側邊欄菜單哪個好
1)最外圍的是一個Activity,頂部包含了一個View的容器,這個容器主要是裝載ToggleButton來實現諸如美團裡面的「美食,全城,理我最近,刷選」這一行。這一行一點就會彈出對應的下來菜單。
2)下拉菜單是如何實現的呢?,這里我們利用了PopupWindow來實現這一彈出式窗口。然後我們在彈出式窗口裡面再定義我們的下來列表項,是單列還是二級菜單,都是由裡面來定。
3)不同的菜單,需要一級或者需要二級,在這里根據我的需求而變動。我們在PopupWindow上面加一個自定義的LeftView,或者是MiddleView,RightView。主要是一個ToggleButton,你彈出一個窗口,你就定製一個窗口。
3)視圖裡面嵌入ListView,就形成了列表項。
5. android 怎麼實現左側導航欄
Android左側推出導航菜單可以讓Activity繼承PopupWindow類來實現的彈出窗體,布局可以根據自己定義設計。彈出效果主要使用了translate和alpha樣式實現。具體的做法是下列代碼:
第一步:設計彈出窗口xml:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="從相冊選擇"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
第二步:創建SelectPicPopupWindow類繼承PopupWindow:
java代碼
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
//取消按鈕
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//銷毀彈出框
dismiss();
}
});
//設置按鈕監聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//設置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//設置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
//設置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//設置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設置SelectPicPopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.AnimBottom);
//實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
}
第三步:編寫MainActivity類實現測試:
Java代碼
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//自定義的彈出框類
SelectPicPopupWindow menuWindow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) this.findViewById(R.id.text);
//把文字控制項添加監聽,點擊彈出自定義窗口
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//實例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//顯示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //設置layout在PopupWindow中顯示的位置
}
});
}
//為彈出窗口實現監聽類
private OnClickListener itemsOnClick = new OnClickListener(){
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo:
break;
case R.id.btn_pick_photo:
break;
default:
break;
}
}
};
}
上述的代碼實現了從底部彈出,也可以根據PopupWindow類設置從左下部彈出。
Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在於:
AlertDialog的位置固定,而PopupWindow的位置可以隨意
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的
PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移
6. android系統的二級菜單是在哪個版本的時候被去掉的
4.4
7. 我想在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.}
8. android7.0和6.0有什麼區別
安卓7.0通知欄相較Android 6.0的變化還是很大的。首先,一級下拉頂部的用戶頭像取消,時間日期合並為一行,同時新增快捷圖標,例如WiFi、信號、電池等,另外通知條幅樣式也有所變化,清除按鈕也翻轉了一下。
安卓7.0的二級菜單布局也有所變化,更加密集,同時新增了編輯按鈕和多頁面排版,用戶可擺放的快捷鍵就更多了。
安卓7.0和6.0相比,安裝軟體速度更快,打開軟體速度更快,運行軟體速度更快。
9. android 怎麼實現左側推出導航菜單
Android左側推出導航菜單可以讓Activity繼承PopupWindow類來實現的彈出窗體,布局可以根據自己定義設計。彈出效果主要使用了translate和alpha樣式實現。具體的做法是下列代碼:
第一步:設計彈出窗口xml:
Xml代碼
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="從相冊選擇"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
第二步:創建SelectPicPopupWindow類繼承PopupWindow:
Java代碼
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.drawable.ColorDrawable;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
{
privateButtonbtn_take_photo,btn_pick_photo,btn_cancel;
privateViewmMenuView;
publicSelectPicPopupWindow(Activitycontext,OnClickListeneritemsOnClick){
super(context);
LayoutInflaterinflater=(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView=inflater.inflate(R.layout.alert_dialog,null);
btn_take_photo=(Button)mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo=(Button)mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel=(Button)mMenuView.findViewById(R.id.btn_cancel);
//取消按鈕
btn_cancel.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//銷毀彈出框
dismiss();
}
});
//設置按鈕監聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//設置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//設置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
//設置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//設置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設置SelectPicPopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.AnimBottom);
//實例化一個ColorDrawable顏色為半透明
ColorDrawabledw=newColorDrawable(0xb0000000);
//設置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mMenuView.setOnTouchListener(newOnTouchListener(){
publicbooleanonTouch(Viewv,MotionEventevent){
intheight=mMenuView.findViewById(R.id.pop_layout).getTop();
inty=(int)event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
returntrue;
}
});
}
}
第三步:編寫MainActivity類實現測試:
Java代碼
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.TextView;
{
//自定義的彈出框類
;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextViewtv=(TextView)this.findViewById(R.id.text);
//把文字控制項添加監聽,點擊彈出自定義窗口
tv.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//實例化SelectPicPopupWindow
menuWindow=newSelectPicPopupWindow(MainActivity.this,itemsOnClick);
//顯示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main),Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);//設置layout在PopupWindow中顯示的位置
}
});
}
//為彈出窗口實現監聽類
=newOnClickListener(){
publicvoidonClick(Viewv){
menuWindow.dismiss();
switch(v.getId()){
caseR.id.btn_take_photo:
break;
caseR.id.btn_pick_photo:
break;
default:
break;
}
}
};
}
上述的代碼實現了從底部彈出,也可以根據PopupWindow類設置從左下部彈出。
Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在於:
AlertDialog的位置固定,而PopupWindow的位置可以隨意
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的
PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移