編輯:關於 Android 開發
PopupWindow 可在一個 view 之上浮現一個彈出視窗,常用於實作自訂的下拉選單或浮動操作面板。本文以一個包含分享、檢舉、取消三個選項的範例,示範 PopupWindow 的完整用法。
首先在 main.xml 中建立一個觸發用的 Button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout" >
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
接著建立 popup.xml,放入三個 TextView 作為選單項目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dip" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/fenxiang"
android:id="@+id/tv_fenxiang" android:textColor="#000000" android:textSize="30sp" />
<ImageView android:layout_width="fill_parent"
android:layout_height="1dip" android:background="@drawable/imv_line" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/jubao"
android:id="@+id/tv_jubao" android:textColor="#000000" android:textSize="30sp" />
<ImageView android:layout_width="fill_parent"
android:layout_height="1dip" android:background="@drawable/imv_line" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/cancel"
android:id="@+id/tv_cancel" android:textColor="#000000" android:textSize="30sp" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
getPopupWindow();
popup.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM, 0, -70);
// popup.showAsDropDown(findViewById(R.id.layout), 20, -220);
// popup.showAsDropDown(findViewById(R.id.layout));
}
});
}
/** 建立 PopupWindow */
protected void initPopuptWindow() {
View popupWindow_view = getLayoutInflater().inflate(R.layout.popup, null, false);
popup = new PopupWindow(popupWindow_view, 260, 160, true); // 建立 PopupWindow 實例
tv_fenxiang = (TextView)popupWindow_view.findViewById(R.id.tv_fenxiang);
tv_jubao = (TextView)popupWindow_view.findViewById(R.id.tv_jubao);
tv_cancel = (TextView)popupWindow_view.findViewById(R.id.tv_cancel);
popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_bg));
popup.setFocusable(true); // 讓選單取得焦點,否則其中元件的事件無法回應
popup.update();
}
/** 取得 PopupWindow 實例 */
private void getPopupWindow() {
if (null != popup) {
popup.dismiss();
return;
} else {
initPopuptWindow();
}
}
重點在於:以 showAtLocation 或 showAsDropDown 決定彈出位置,並務必呼叫 setFocusable(true) 讓選單取得焦點,否則其中的元件將無法回應點擊事件。透過 getPopupWindow 的判斷,還能實現點擊時在顯示與關閉之間切換。
HelloCharts 圖表框架實戰(三):圓餅圖也能畫得漂亮
HelloCharts 圖表框架實戰(三):圓餅圖也能畫得漂亮 折線圖、柱狀圖之後,這次換個口味聊圓餅圖。HelloCharts 的 API 設計相當親切,一個 PieC
Android 群英傳筆記(一):View 的結構、測量與 onMeasure 重寫
讀完《第一行代碼》之後接著啃《Android 群英傳》,趁記憶還熱先把筆記留下來,一方面鞏固觀念,一方面也方便日後回頭查。 一、View 是什麼 1. 先看 View 的
Android 熱修補動態修復(三):用 Javassist 注入位元碼,打造可用的熱修補框架雛形
一、先說個插曲 這篇原本在 4 月 8 日晚上就發佈了,接著我埋頭寫第四篇,結果把(四)存進草稿箱時,發現已發佈的(三)憑空消失,位置上變成了第四篇。問過論壇版主,可能
Android 網路請求:GET 與 POST 的三種實作方式
GET 與 POST 實作 向伺服器提交資料時,GET 會把參數附加在網址後方,POST 則將資料放入請求主體。GET 較適合查詢,但長度與安全性受限;POST 適合傳送