[英]How to make a pop up image like an advertisement? [Android]
I've encountered a lot of pop up images like the example below, i've made a pop up image but there is no close button at the top left corner, do u guys have any idea how to make a pop up image with close button at the top left cornel like the example below in Android? 我遇到了很多如以下示例所示的弹出图像,我制作了一个弹出图像,但是左上角没有关闭按钮,你们是否有办法制作一个带有关闭图像的弹出图像像Android中的以下示例一样,位于左上角的按钮?
You need to make your own Dialog with your custom layout 您需要使用自定义布局制作自己的对话框
Dialog dialog;
private void showDialog() {
// custom dialog
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
// set the custom dialog components - text, image and button
ImageButton close = (ImageButton) dialog.findViewById(R.id.btnClose);
Button buy = (Button) dialog.findViewById(R.id.btnBuy);
// Close Button
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//TODO Close button action
}
});
// Buy Button
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//TODO Buy button action
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
custom_dialog.xml custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@android:color/white"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/images" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btnBuy"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/holo_green_light"
android:text="BUY"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btnBuy"
android:text="Thank You (Domestic Album Version)"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:text="Still Not Gettin' Any, 2004" />
</RelativeLayout>
</LinearLayout>
<ImageButton
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/black"
android:src="@android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
Result is: 结果是:
You can use this library. 您可以使用此库。 There is close button on top right corner. 右上角有关闭按钮。 only need to set image only. 只需要设置图像而已。
https://github.com/chathuralakmal/AndroidImagePopup https://github.com/chathuralakmal/AndroidImagePopup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.