Android簡單實(shí)現(xiàn)自定義彈框(PopupWindow)
一:一般都是先上效果圖

二:實(shí)現(xiàn)步驟:
1.xml布局實(shí)現(xiàn)
<?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" android:background="@drawable/store_bgimg"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="214dp" android:layout_centerVertical="true" android:layout_marginLeft="31dp" android:layout_marginRight="31dp" android:background="@drawable/tkbjzj"> <TextView android:id="@+id/tetle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="26dp" android:text="七天連鎖酒店" android:textColor="#262626" android:textSize="18dp" /> <TextView android:id="@+id/textdz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tetle" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="你已領(lǐng)取本店7.5折優(yōu)惠券" android:textColor="#ea302e" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_above="@+id/lineardb" android:background="#e6e6e6" /> <LinearLayout android:id="@+id/lineardb" android:layout_width="match_parent" android:layout_height="44dp" android:layout_alignParentBottom="true"> <TextView android:id="@+id/textwzdl" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="我知道了" android:textColor="#262626" android:textSize="16dp" /> <TextView android:id="@+id/textckxq" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="#f86c6a" android:gravity="center" android:text="查看詳情" android:textColor="#ffffff" android:textSize="16dp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
2.drawable文件下的轉(zhuǎn)角,然后在布局引用
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景顏色 --> <solid android:color="#ffffff" /> <!-- 控制邊界線顏色和大小 --> <stroke android:width="1dp" android:color="#ffffff" /> <!-- 控制圓角大小 --> <corners android:radius="4dp" /> </shape>
3.activity的實(shí)現(xiàn)
/**
* 彈框
*/
private View mPopupHeadViewy;//創(chuàng)建一個(gè)view
private PopupWindow mHeadPopupclly;//PopupWindow
private TextView tetle, textdz;//title,打折
private TextView textwzdl, textckxq;//我知道了,查看詳情
@SuppressWarnings("deprecation")
private void popupHeadWindowcll() {
mPopupHeadViewy = View.inflate(getActivity(), R.layout.tankuang_layout, null);
tetle = (TextView) mPopupHeadViewy.findViewById(R.id.tetle);
textdz = (TextView) mPopupHeadViewy.findViewById(R.id.textdz);
textwzdl = (TextView) mPopupHeadViewy.findViewById(R.id.textwzdl);
textckxq = (TextView) mPopupHeadViewy.findViewById(R.id.textckxq);
mHeadPopupclly = new PopupWindow(mPopupHeadViewy, AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT, true);
// 在PopupWindow里面就加上下面代碼,讓鍵盤彈出時(shí),不會擋住pop窗口。
mHeadPopupclly.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mHeadPopupclly.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mHeadPopupclly.setBackgroundDrawable(new BitmapDrawable());
mHeadPopupclly.setOutsideTouchable(true);
mHeadPopupclly.showAsDropDown(textviewid, 0, 0);
textwzdl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHeadPopupclly.dismiss();
}
});
textckxq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHeadPopupclly.dismiss();
Toast.makeText(getActivity(), "查看詳情", Toast.LENGTH_LONG).show();
}
});
}
注意:
1、
mHeadPopupclly = new PopupWindow(mPopupHeadViewy, AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT, true);
這句代碼就是控制彈出框是鋪滿屏幕還是自適應(yīng)
2、
mHeadPopupclly.showAsDropDown(textviewid, 0, 0);
這句話是這個(gè)彈框基于哪個(gè)控件之下,textviewid是控件名,后面兩個(gè)是坐標(biāo)
這是一個(gè)簡單的自定義彈框,大神勿噴,有用的希望頂一下
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android高德地圖marker自定義彈框窗口
- Android自定義彈框樣式
- Android啟動頁用戶相關(guān)政策彈框的實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)抖音頭像底部彈框效果的實(shí)例代碼
- Android中 TeaScreenPopupWindow多類型篩選彈框功能的實(shí)例代碼
- android自定義Dialog彈框和背景陰影顯示效果
- Android 提交或者上傳數(shù)據(jù)時(shí)的dialog彈框動畫效果
- 淺析Android中常見三種彈框在項(xiàng)目中的應(yīng)用
- Android填坑系列:在小米系列等機(jī)型上放開定位權(quán)限后的定位請求彈框示例
- Android自定義彈框Dialog效果
相關(guān)文章
Android程序開發(fā)之動態(tài)設(shè)置ImageView的亮度
這篇文章主要介紹了Android程序開發(fā)之動態(tài)設(shè)置ImageView的亮度 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android 新手引導(dǎo)蒙層效果實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android 新手引導(dǎo)蒙層效果實(shí)現(xiàn)代碼示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android應(yīng)用第一次安裝成功點(diǎn)擊“打開”后Home鍵切出應(yīng)用后再點(diǎn)擊桌面圖標(biāo)返回導(dǎo)致應(yīng)用重啟問題的解決方法
這篇文章主要介紹了Android應(yīng)用第一次安裝成功點(diǎn)擊“打開”后Home鍵切出應(yīng)用后再點(diǎn)擊桌面圖標(biāo)返回導(dǎo)致應(yīng)用重啟問題的解決方法,需要的朋友可以參考下2016-11-11
Android Service中使用Toast無法正常顯示問題的解決方法
這篇文章主要介紹了Android Service中使用Toast無法正常顯示問題的解決方法,分析了Service中Toast無法正常顯示的原因與相關(guān)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10

