自定义弹出窗体
1.创建自定义popupwindow对象
2.设置宽度 高度 布局
3.显示 (两种方式)
(1)showAsDropDown:在控件的下方
(2)showAtLocation:从手机的上下左右的方向弹出
颜色会随着你点击的按钮变化而变化
//主类
public class MainActivity extends AppCompatActivity {
Button button;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.iv);
linearLayout=findViewById(R.id.lil);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
diypopupwindow diypopupwindow = new diypopupwindow(MainActivity.this);
diypopupwindow.setWidth(300);
diypopupwindow.setHeight(550);
diypopupwindow.setOutsideTouchable(true);
diypopupwindow.setRedListener(new RedListener() {
@Override
public void red() {
linearLayout.setBackgroundColor(Color.RED);
}
});
diypopupwindow.setBlueListener(new BlueListener() {
@Override
public void blue() {
linearLayout.setBackgroundColor(Color.BLUE);
}
});
diypopupwindow.setGreenListener(new GreenListener() {
@Override
public void green() {
linearLayout.setBackgroundColor(Color.GREEN);
}
});
//弹出窗体 背景半透明 窗体消失 恢复
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.5f;
getWindow().setAttributes(attributes);
//弹出窗体 背景半透明 窗体消失 恢复
diypopupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=1f;
getWindow().setAttributes(attributes);
}
});
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, null);
popupWindow.showAsDropDown(view);
// popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);//正下方显示
}
});
}
}
//自定义popupWindow类
public class diypopupwindow extends PopupWindow {
Context mcontext;
RedListener redListener;
GreenListener greenListener;
BlueListener blueListener;
public void setRedListener(RedListener redListener) {
this.redListener = redListener;
}
public void setGreenListener(GreenListener greenListener) {
this.greenListener = greenListener;
}
public void setBlueListener(BlueListener blueListener) {
this.blueListener = blueListener;
}
public diypopupwindow(Context context) {
super(context);
this.mcontext = context;
View view = LayoutInflater.from(mcontext).inflate(R.layout.item_layout, null);
setContentView(view);
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Button btn1 = view.findViewById(R.id.btn1);
Button btn2 = view.findViewById(R.id.btn2);
Button btn3 = view.findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
redListener.red();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
blueListener.blue();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenListener.green();
}
});
}
}
//接口回调
//三个接口
public interface BlueListener {
public void blue();
}
public interface GreenListener {
public void green();
}
public interface RedListener {
public void red();
}
自定义对话框
//1.加载布局 2.初始化布局的空间 3.为空间加载数据 4.设置监听
//自定义对话框类
public class diydialog extends Dialog {
ImageView imageView;
TextView textView;
Button button1,button2;
int img;
String message;
String no_txt,yes_txt;
NoListenr noListenr;
YesListenr yesListenr;
public void setNoListenr(NoListenr noListenr) {
this.noListenr = noListenr;
}
public void setYesListenr(YesListenr yesListenr) {
this.yesListenr = yesListenr;
}
public void setImg(int img) {
this.img = img;
}
public void setMessage(String message) {
this.message = message;
}
public void setNo_txt(String no_txt) {
this.no_txt = no_txt;
}
public void setYes_txt(String yes_txt) {
this.yes_txt = yes_txt;
}
public diydialog(Context context) {
super(context);
}
public diydialog(Context context, int themeResId) {
super(context, themeResId);
}
protected diydialog(Context context, boolean cancelable,DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diy_layout);
//1.加载布局 2.初始化布局的空间 3.为空间加载数据
//4.设置监听
imageView=findViewById(R.id.iv);
textView=findViewById(R.id.tv);
button1=findViewById(R.id.no);
button2=findViewById(R.id.ok);
imageView.setImageResource(img);
textView.setText(message);
button1.setText(no_txt);
button2.setText(yes_txt);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noListenr.no();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesListenr.yes();
}
});
}
}
//主类
public class MainActivity extends AppCompatActivity {
Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.btn1);
button2=findViewById(R.id.btn2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final diydialog diydialog = new diydialog(MainActivity.this);
diydialog.setImg(R.mipmap.ic_launcher_round);
diydialog.setMessage("怎么回事啊老弟");
diydialog.setNo_txt("cancel");
diydialog.setYes_txt("ok");
diydialog.setNoListenr(new NoListenr() {
@Override
public void no() {
Toast.makeText(MainActivity.this,"您选择了cancel",Toast.LENGTH_SHORT).show();
diydialog.dismiss();
}
});
diydialog.setYesListenr(new YesListenr() {
@Override
public void yes() {
Toast.makeText(MainActivity.this,"您选择了ok",Toast.LENGTH_SHORT).show();
diydialog.dismiss();
}
});
diydialog.show();
}
});
}
}
//接口回调
//两个接口
public interface NoListenr {
public void no();
}
public interface YesListenr {
public void yes();
}
//自定义对话框的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="240dp">
<ImageView
android:layout_gravity="center"
android:id="@+id/iv"
android:src="@mipmap/ic_launcher_round"
android:layout_width="200dp"
android:layout_height="100dp"/>
<TextView
android:id="@+id/tv"
android:textSize="20dp"
android:text="你好"
android:gravity="center"
android:layout_width="200dp"
android:layout_height="30dp"/>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="100dp">
<Button
android:id="@+id/no"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="取消"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button
android:id="@+id/ok"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="确定"
android:layout_width="100dp"
android:layout_height="40dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
//主类XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="diy对话框"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
poupwindow菜单
//主类
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);//充满屏幕
popupWindow.setHeight(500);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, null);
popupWindow.setContentView(view);
//弹出窗体 背景半透明 窗体消失 恢复
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=1f;
getWindow().setAttributes(attributes);
}
});
popupWindow.setOutsideTouchable(true);
//设置动画
popupWindow.setAnimationStyle(R.style.mystyle);
popupWindow.showAtLocation(LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null), Gravity.BOTTOM,0,0);
}
});
}
}
//主类xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="@+id/iv"
android:src="@mipmap/ic_launcher_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
//菜单xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/tv1"
android:text="扫一扫"
android:textSize="25dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/tv2"
android:text="哈哈哈"
android:textSize="25dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/tv3"
android:text="略略略"
android:textSize="25dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
//style文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
//需要自己定义动画的style
<style name="mystyle">
<item name="android:windowEnterAnimation">@anim/popup_in</item>
<item name="android:windowExitAnimation">@anim/popup_out</item>
</style>
</resources>
//在res下创建anim文件夹需要弹出和消失的动画xml文件
//out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="100%"
android:fromYDelta="0"
android:duration="2000"
></translate>
</set>
//in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="0"
android:fromYDelta="100%"
android:duration="2000"
></translate>
</set>