本文實例講述了Android編程實現(xiàn)長按彈出選項框View進行操作的方法。分享給大家供大家參考,具體如下:
長按彈出選項框View進行操作
主要代碼解釋
private void showPopWindows(View v) { /** pop view */ View mPopView = LayoutInflater.from(this).inflate(R.layout.popup, null); final PopupWindow mPopWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); /** set */ mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); /** 這個很重要 ,獲取彈窗的長寬度 */ mPopView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); int popupWidth = mPopView.getMeasuredWidth(); int popupHeight = mPopView.getMeasuredHeight(); /** 獲取父控件的位置 */ int[] location = new int[2]; v.getLocationOnScreen(location); /** 顯示位置 */ mPopWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight); mPopWindow.update(); final String copyTxt = (String) v.getTag(); mPopView.findViewById(R.id.tv_copy_txt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { copyToClip(copyTxt); if (mPopWindow != null) { mPopWindow.dismiss(); } } });}layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/pop_bg" > <TextView android:id="@+id/tv_copy_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:gravity="center" android:text="復(fù)制邀請碼" android:textColor="@android:color/white" android:textSize="12sp" /></LinearLayout>
效果圖:

根據(jù)上面可以自行調(diào)整位置。
完整實例代碼點擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點
疑難解答