简体   繁体   中英

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?

图片弹出示例

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

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM