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

源代碼地址:https://github.com/luoye123/Box
東西很簡單,我就直接亮代碼了:
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: //點擊拍照按鈕
break;
case R.id.item_popupwindows_Photo: //點擊從相冊中選擇按鈕
break;
default:
break;
}
}
};
}
3、關(guān)鍵代碼:SelectPicPopupWindow.java
**public class SelectPicPopupWindow extends PopupWindow {
private Button item_popupwindows_camera, //彈窗拍照按鈕
item_popupwindows_Photo, //彈窗從相冊選擇按鈕
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); //圖庫按鈕
/**
* 取消按鈕銷毀事件
*/
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彈出窗體的高
//修改高度顯示,解決被手機底部虛擬鍵擋住的問題 by黃海杰 at:2015-4-30
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//設(shè)置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設(shè)置SelectPicPopupWindow彈出窗體動畫效果
//this.setAnimationStyle(R.style);
//實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設(shè)置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//menuview添加ontouchlistener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
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;
}
});
}
}**
寫的不好,請見諒,,下一期完成后期的工作!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自帶倒計時控件Chronometer使用方法詳解
這篇文章主要為大家詳細介紹了Android自帶倒計時控件Chronometer的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解
這篇文章主要介紹了Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android SharedPreference存儲文件三步走
SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出2023-01-01
Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01
關(guān)于AndroidStudio新建與編譯項目速度慢解決辦法
這篇文章主要介紹了關(guān)于AndroidStudio新建與編譯項目速度慢的解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

