一元践、PopupWindow的簡介
java.lang.Object
?
android.widget.PopupWindow
PopupWindow:彈出框。在activity上需要的地方
顯示該控件童谒。
二单旁、自定義PopupWindow的使用
1、自定義PopupWindow的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_take_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="視頻"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="取消"/>
<TextView
android:id="@+id/tv_take_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="照片"
/>
</LinearLayout>
可以用ListView饥伊、RecyclerView象浑,需要多寫一個Item布局
2、自定義PopupWindow繼承于PopupWindow
package com.example.administrator.popwindow;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
/**
* creat by Ann
* creat on ${DATA}
* description
*/
public class ChooseFilePopupwindow extends PopupWindow implements View.OnClickListener {
/** 上下文 */
private Activity mActivity;
/** 根布局 */
private View mRootView;
private TextView mTvTakePicture;
private TextView mTvTakeVideo;
private TextView mTvCancel;
private ChooseFilePopViewClickListener mChooseFilePopViewClickListener;
public ChooseFilePopupwindow(Activity activity) {
this.mActivity = activity;
initConfig(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
initView();
}
public ChooseFilePopupwindow(Activity activity, int width, int height) {
this.mActivity = activity;
initConfig(width, height);
initView();
}
@SuppressLint("NewApi")
private void initConfig(int width, int height) {
this.setWidth(width);
this.setHeight(height);
this.setFocusable(true);
this.setTouchable(true);
this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
this.setOutsideTouchable(true);
this.setElevation(25f);
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//達到背景全部變暗的效果
// mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
// WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// this.setAnimationStyle(R.style.BottomPopuAnim);
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
mActivity.getWindow().setDimAmount(0f);
params.alpha = 1.0f;
mActivity.getWindow().setAttributes(params);
}
});
}
private void initView() {
mRootView = LayoutInflater.from(mActivity).inflate(R.layout.popupwindow_choose_file, null, false);
mTvCancel = mRootView.findViewById(R.id.tv_cancel);
mTvTakePicture = mRootView.findViewById(R.id.tv_take_picture);
mTvTakeVideo = mRootView.findViewById(R.id.tv_take_video);
mTvCancel.setOnClickListener(this);
mTvTakePicture.setOnClickListener(this);
mTvTakeVideo.setOnClickListener(this);
setContentView(mRootView);
}
/**
* 顯示
* @param parent 關(guān)聯(lián)父布局
*/
public void show(View parent) {
WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
params.alpha = 0.7f;
mActivity.getWindow().setDimAmount(0.7f);
mActivity.getWindow().setAttributes(params);
this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_take_picture:
dismiss();
if(mChooseFilePopViewClickListener != null){
mChooseFilePopViewClickListener.takePicture();
}
break;
case R.id.tv_take_video:
dismiss();
if(mChooseFilePopViewClickListener != null){
mChooseFilePopViewClickListener.takeVideo();
}
break;
case R.id.tv_cancel:
dismiss();
break;
}
}
public void setChooseFilePopViewClickListener(ChooseFilePopViewClickListener mChooseFilePopViewClickListener) {
this.mChooseFilePopViewClickListener = mChooseFilePopViewClickListener;
}
public interface ChooseFilePopViewClickListener{
public void takePicture();
public void takeVideo();
}
}
3、在activity中顯示PopupWindow(可以自行選擇,不適用回調(diào)方法)
package com.example.administrator.popwindow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn_pop);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChooseFilePopupwindow popwindow = new ChooseFilePopupwindow(MainActivity.this);
popwindow.show(button);
popwindow.setChooseFilePopViewClickListener(new ChooseFilePopupwindow.ChooseFilePopViewClickListener() {
@Override
public void takePicture() {
Toast.makeText(MainActivity.this,"點擊了拍照",Toast.LENGTH_LONG).show();
}
@Override
public void takeVideo() {
Toast.makeText(MainActivity.this,"點擊了攝像",Toast.LENGTH_LONG).show();
}
});
}
});
}
}
總結(jié):想好popupwindow樣式進行布局---->自定義popupwindow繼承于popupwindow壹甥,做好邏輯的處理(初始化,位置翰蠢,大小,背景是否變暗,popupwindow的Item點擊事件。)------>在activity中創(chuàng)建并顯示驰贷。