Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 菜單半透明效果實作:Shape + GridView + PopupWindow 三步驟

編輯:關於 Android 程式設計

用過「天天動聽」的朋友,大概都對它那層半透明的彈出菜單有印象:浮在介面上方,隱隱透出背後的內容,質感相當細膩。本文就以這種效果為目標,把整個實作過程拆解開來,並附上可以直接跑起來的程式碼。

先把需求拆乾淨

動手之前釐清要做的事,後面寫code會順很多:

  1. 用 Shape 定義一張半透明的圓角底板,當作菜單的背景。
  2. 撰寫菜單的版面配置,核心是一個 GridView,所有圖示都放進這個網格裡。
  3. 決定菜單的承載方式:PopupWindow、AlertDialog,或是一個背景透明的 Activity 都可以。
  4. 替網格項目掛上點擊監聽(示例中沒有實作,需要的話在自己的 Adapter 裡補一段即可)。

整體難度其實不高,真正的細節都在樣式上。

半透明圓角背景

關鍵在 solid 的顏色值。把 alpha 通道寫成 b4,黑色就會變成約七成不透明的遮罩;再搭配 stroke 的虛線描邊與 corners 的圓角,就能做出近似系統對話框的質感。

 <?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> 

菜單的版面配置

這層很單純:一個直向的 LinearLayout 包住 GridView,背景指向剛才那張 Shape。numColumns="auto_fit" 搭配固定的 columnWidth,可以讓網格列數依實際項目數量自動伸縮,不用自己算。

 <?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> 

主要程式碼

這裡刻意把 AlertDialog 的寫法留在註解裡對照。兩種做法的差别在於:AlertDialog 會自帶一層系統遮罩與視窗動畫,而 PopupWindow 完全交給你自己控制,因此更容易做出「浮在畫面上、背後若隱若現」的观感。

 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 彈出菜單
 // 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 彈出菜單
 pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
 // 第一個參數必須傳入一個存在的 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;
 }
 }
 } 

幾個實作上的提醒

Adapter 這裡是用程式動態組出 LinearLayout,好處是不必為每個項目準備額外的配置檔;缺點是圖示與文字都寫死在程式裡,日後要擴充得改 code。如果項目樣式會變動,改成 XML 配置加 LayoutInflater 會更好維護。

另外 showAtLocation() 的第一個參數不能傳 null,它只是用來取得視窗 token 的錨點 View,隨便找一個已經掛在畫面上的元件即可。位置參數則建議依實際的狀態列高度微調,寫死數值在不同解析度下容易偏移。

看到這裡你應該會發現,半透明菜單本身並不複雜,真正花時間的是配色、間距與圓角這些細節。拿這份範例當骨架,替換成自己的圖示與文案,就能調展出更順眼的版本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved