Android實(shí)現(xiàn)底部彈窗效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)底部彈窗效果的具體代碼,供大家參考,具體內(nèi)容如下

源代碼地址:https://github.com/luoye123/Box
東西很簡(jiǎn)單,我就直接亮代碼了:
1、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:id="@+id/ll_image"> <Button android:id="@+id/bt_select_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇圖片" /> </LinearLayout>
2、MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private SelectPicPopupWindow menuWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_select_image).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_select_image:
//TODO implement
selectImgs();
}
}
private void selectImgs(){
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//設(shè)置彈窗位置
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.ll_image), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
private View.OnClickListener itemsOnClick = new View.OnClickListener() {
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.item_popupwindows_camera: //點(diǎn)擊拍照按鈕
break;
case R.id.item_popupwindows_Photo: //點(diǎn)擊從相冊(cè)中選擇按鈕
break;
default:
break;
}
}
};
}
3、關(guān)鍵代碼:SelectPicPopupWindow.java
**public class SelectPicPopupWindow extends PopupWindow {
private Button item_popupwindows_camera, //彈窗拍照按鈕
item_popupwindows_Photo, //彈窗從相冊(cè)選擇按鈕
item_popupwindows_cancel; //彈窗取消按鈕
private View menuview;
/**
* 上傳圖片*************************
* @param context
* @param itemsOnclick
*/
public SelectPicPopupWindow(Activity context, View.OnClickListener itemsOnclick){
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menuview = inflater.inflate(R.layout.item_popupwindows,null);
item_popupwindows_camera = (Button) menuview.findViewById(R.id.item_popupwindows_camera); //拍照按鈕
item_popupwindows_cancel = (Button) menuview.findViewById(R.id.item_popupwindows_cancel); //取消按鈕
item_**popupwindows_Photo = (Button) menuview.findViewById(R.id.item_popupwindows_Photo); //圖庫(kù)按鈕
/**
* 取消按鈕銷毀事件
*/
item_popupwindows_cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
dismiss();
}
});
item_popupwindows_camera.setOnClickListener(itemsOnclick);
item_popupwindows_Photo.setOnClickListener(itemsOnclick);
//設(shè)置SelectPicPopupWindow的View
this.setContentView(menuview);
//設(shè)置SelectPicPopupWindow**彈出窗體的寬
this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
//設(shè)置SelectPicPopupWindow彈出窗體的高
//修改高度顯示,解決被手機(jī)底部虛擬鍵擋住的問(wèn)題 by黃海杰 at:2015-4-30
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊
this.setFocusable(true);
//設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫(huà)效果
//this.setAnimationStyle(R.style);
//實(shí)例化一個(gè)ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設(shè)置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//menuview添加ontouchlistener監(jiān)聽(tīng)判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
menuview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
int height = menuview.findViewById(R.id.ll_popup).getTop();
int y = (int) motionEvent.getY();
if (motionEvent.getAction() == MotionEvent.ACTION_UP){
if (y<height){
dismiss();
}
}
return true;
}
});
}
}**
寫(xiě)的不好,請(qǐng)見(jiàn)諒,,下一期完成后期的工作!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android自帶倒計(jì)時(shí)控件Chronometer的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Kotlin之在Gradle中無(wú)參(no-arg)編譯器插件的使用詳解
這篇文章主要介紹了Kotlin之在Gradle中無(wú)參(no-arg)編譯器插件的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android SharedPreference存儲(chǔ)文件三步走
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出2023-01-01
Android編程開(kāi)發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開(kāi)發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01
關(guān)于AndroidStudio新建與編譯項(xiàng)目速度慢解決辦法
這篇文章主要介紹了關(guān)于AndroidStudio新建與編譯項(xiàng)目速度慢的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

