Android Menu半透明效果的開(kāi)發(fā)實(shí)例
不知道大家是否用過(guò)天天動(dòng)聽(tīng),對(duì)于它界面上的半透明Menu效果,筆者感覺(jué)非常漂亮。下面是天天動(dòng)聽(tīng)半透明Menu的截圖,欣賞下吧:

感覺(jué)還不錯(cuò)吧?那么如何實(shí)現(xiàn)這種半透明Menu效果呢?本文就重點(diǎn)討論并給出這種Menu的具體代碼實(shí)現(xiàn)過(guò)程。
首先分析下實(shí)現(xiàn)這種半透明Menu所需做的工作,并進(jìn)行合理分解:
1. 利用Shaper設(shè)置一個(gè)半透明圓角背景。
2. 定義Menu布局,主要就GridView,把圖標(biāo)都放在這個(gè)GridView。
3. Menu事件, 通過(guò)PopupWindow或者AlertDialog或者透明Activity顯示到頁(yè)面即可。
4. 按鈕的監(jiān)聽(tīng)事件,實(shí)例中沒(méi)加。需要的話(huà)自己在Adapter里加。
比較簡(jiǎn)單,不多說(shuō)了。
半透明圓角背景xml:
XML/HTML代碼
<?xml version="1.0" encoding="UTF-8"?> <shape android:shape="rectangle"> <solid android:color="#b4000000" /> <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" /> <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" /> <corners android:radius="8.0dip" /> </shape>
Menu布局:
XML/HTML代碼
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<GridView android:gravity="center"
android:layout_gravity="center"
android:id="@+id/menuGridChange"
android:background="@drawable/menu_bg_frame"
android:padding="5.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10.0dip"
android:verticalSpacing="3.0dip"
android:stretchMode="columnWidth"
android:columnWidth="60.0dip"
android:numColumns="auto_fit"/>
</LinearLayout>
主要類(lèi):
Java代碼
package com.yfz;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class MenuTest extends Activity {
private String TAG = this.getClass().getSimpleName();
private int[] resArray = new int[] {
R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,
R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan
};
private String[] title = new String[]{
"添加歌曲", "歌曲信息", "查找歌詞", "搜索歌詞"
};
private static boolean show_flag = false;
private PopupWindow pw = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e(TAG, "------ onCreateOptionsMenu ------");
//用AlertDialog彈出menu
// View view = LayoutInflater.from(this).inflate(R.layout.menu, null);
// GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
// grid1.setAdapter(new ImageAdapter(this));
// Builder build = new AlertDialog.Builder(this);
// build.setView(view);
// build.show();
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.menu, null);
GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
grid1.setAdapter(new ImageAdapter(this));
//用Popupwindow彈出menu
pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//NND, 第一個(gè)參數(shù), 必須找個(gè)View
pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return resArray.length;
}
@Override
public Object getItem(int arg0) {
return resArray[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LinearLayout linear = new LinearLayout(context);
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(context);
iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.gravity=Gravity.CENTER;
linear.addView(iv, params2);
TextView tv = new TextView(context);
tv.setText(title[arg0]);
LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params3.gravity=Gravity.CENTER;
linear.addView(tv, params3);
return linear;
}
}
}
到此,大家是不是覺(jué)得半透明Menu效果也是比較好實(shí)現(xiàn)的呢?可以根據(jù)自己的需要對(duì)此實(shí)例進(jìn)行修改以求更美觀好用。
以上就是對(duì)Android Menu 半透明效果的實(shí)現(xiàn),后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對(duì)本站的支持!
- Android仿Iphone屏幕底部彈出半透明PopupWindow效果
- Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
- Android中設(shè)置組件半透明和透明的效果示例
- Android編程自定義圓角半透明Dialog的方法
- Android開(kāi)發(fā)中Dialog半透明背景消失
- Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法詳解
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法
- Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄(Translucent Bars)
相關(guān)文章
Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法【相互調(diào)用參數(shù)、傳值】
這篇文章主要介紹了Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法,可實(shí)現(xiàn)基于WebView與JavaScript相互調(diào)用參數(shù)、傳值的功能,需要的朋友可以參考下2017-03-03
Android 自定義手勢(shì)--輸入法手勢(shì)技術(shù)
這篇文章主要介紹了Android 自定義手勢(shì)--輸入法手勢(shì)技術(shù)的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android存儲(chǔ)訪問(wèn)框架的使用小結(jié)
這篇文章主要介紹了Android存儲(chǔ)訪問(wèn)框架的使用,存儲(chǔ)訪問(wèn)框架API和MediaStore?API的差異,在于存儲(chǔ)訪問(wèn)框架API,是基于系統(tǒng)文件選擇框的,用戶(hù)選擇了文件,那么相當(dāng)于授權(quán)了,?可以訪問(wèn)所有類(lèi)型的文件,需要的朋友可以參考下2022-01-01
Android自定義ViewPagerIndicator實(shí)現(xiàn)炫酷導(dǎo)航欄指示器(ViewPager+Fragment)
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewPagerIndicator實(shí)現(xiàn)炫酷導(dǎo)航欄指示器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android實(shí)現(xiàn)支付寶手勢(shì)密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶手勢(shì)密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

