歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 自定義Menu

Android 自定義Menu

日期:2017/3/1 9:56:18   编辑:Linux編程

接上一篇Android 滾動Tab http://www.linuxidc.com/Linux/2013-06/85403.htm

Android的Menu鍵, 逐漸淡出歷史舞台, 請翻牆看看Say Goodbye to the Menu button. Menu鍵消失不意味著Menu功能的消失, 恰恰相反Menu功能在Action Bar上面得到更廣闊的發展. 效果如下:

自定義Menu都是使用自定義的PopupWindow或者AlertDialog代替傳統的Menu.

這裡我使用了ActionProvider+PopupWindow實現自定義Menu. 這也是Google官方推薦的方式.

--------------------------------------------------------------------------------

在Activity中

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


menu的布局main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:id="@+id/action_settings"

android:orderInCategory="100"

android:showAsAction="ifRoom"

android:actionProviderClass="com.lichen.remind.actionbar.BlinkActionProvider"

android:title="@string/action_settings"/>

</menu>


--------------------------------------------------------------------------------

自定義布局文件blink_action_provider.xml, 目標是加載到MenuItem的位置.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<ImageView android:id="@+id/menu_blink"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:contentDescription="@string/menu_blink"/>

</RelativeLayout>


這裡的布局只是一個圖片. 可以給它添加Listener, 然後動態添加PopupWindow.

android:actionProviderClass="com.lichen.remind.actionbar.BlinkActionProvider", 需要繼承ActionProvider, 實現其onCreateActionView().

public class BlinkActionProvider extends ActionProvider implements OnClickListener {

private Context mContext;

private LayoutInflater mLayoutInflater;

private PopupWindow mPopWindow;

// 注意構造,需要super(context);

public BlinkActionProvider(Context context) {

super(context);

mContext = context;

}

@Override

@Deprecated

public View onCreateActionView() {

mLayoutInflater = LayoutInflater.from(mContext);

View rootView = mLayoutInflater.inflate(R.layout.blink_action_provider,

null);

ImageView menuBlink = (ImageView) rootView

.findViewById(R.id.menu_blink);

menuBlink.setBackgroundResource(R.drawable.blink_menu);

menuBlink.setOnClickListener(this);

return rootView;

}

@Override

public void onClick(View view) {

/** 自定義PopupWindow */

ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(

R.layout.fragment_about_me, null, true);

mPopWindow = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, true);

// 設置背景透明色

mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

/**設置背景圖

mPopWindow.setBackgroundDrawable(mContext.getResources().getDrawable(

R.drawable.balloon));*/

mPopWindow.setOutsideTouchable(true);// 設置觸摸外面時消失

mPopWindow.setAnimationStyle(android.R.style.Animation_Dialog);// 設置動畫效果

mPopWindow.showAsDropDown(view);// 顯示位置在錨點view的左邊底部

/** 點擊TextView */

TextView tv = (TextView) menuView.findViewById(R.id.about_me);

tv.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Toast.makeText(mContext, "點擊了BlinkMenu", Toast.LENGTH_SHORT)

.show();

mPopWindow.dismiss();

}

});

}

}


這裡的R.layout.fragment_about_me,R.id.about_me請參考上一篇的布局文件 http://www.linuxidc.com/Linux/2013-06/85403.htm

--------------------------------------------------------------------------------

其實, 不僅關注技術, 可以更多的關注設計理念. 如Menu的變化趨勢.


真正好的設計, 我以為是需要有對Android足夠深入的理解, 而不是僅僅PS幾張圖。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved