如何在Android中以编程方式创建弹出窗口

时间:2019-02-01 07:33:18

标签: java android android-layout android-gridlayout android-popupwindow

我进行了“活动”,该活动将在单击按钮时弹出。我必须在“图像按钮”上添加侦听器。我已经在XMl中进行了设计,但是我想以编程方式进行设计。

这是我对xml设计的印象。

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".PopupMenu">
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:orientation="horizontal"
        android:rowCount="1">
        <ImageView
            android:id="@+id/imageOnlineIcon"
            android:layout_width="28dp"
            android:layout_height="28dp"
            app:srcCompat="@drawable/olnine_user"
            android:layout_row="0"
            android:layout_column="0" />
        <TextView
            android:id="@+id/textViewUserName"
            android:layout_width="201dp"
            android:layout_height="44dp"
            android:text="Shyam"
            android:textAlignment="center"
            android:textSize="22sp" />
        <ImageButton
            android:id="@+id/imageVideoIcon"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/videonline" />
        <ImageButton
            android:id="@+id/imageMuteIcon"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/muted" />
    </GridLayout>
    <View style="@style/Divider"/>
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:orientation="horizontal"
        android:rowCount="1">
        <ImageView
            android:id="@+id/imageOnlineIcon1"
            android:layout_width="28dp"
            android:layout_height="28dp"
            app:srcCompat="@drawable/olnine_user" />
        <TextView
            android:id="@+id/textViewUserName1"
            android:layout_width="201dp"
            android:layout_height="44dp"
            android:text="Samir"
            android:textAlignment="center"
            android:textSize="22sp" />
        <ImageButton
            android:id="@+id/imageVideoIcon1"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/videonline" />
        <ImageButton
            android:id="@+id/imageMuteIcon1"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/muted" />
    </GridLayout>
    <View style="@style/Divider"/>
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:orientation="horizontal"
        android:rowCount="1">
        <ImageView
            android:id="@+id/imageOnlineIcon2"
            android:layout_width="28dp"
            android:layout_height="28dp"
            app:srcCompat="@drawable/olnine_user" />
        <TextView
            android:id="@+id/textViewUserName2"
            android:layout_width="201dp"
            android:layout_height="44dp"
            android:text="Vivek"
            android:textAlignment="center"
            android:textSize="22sp" />
        <ImageButton
            android:id="@+id/imageVideoIcon2"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/videonline" />
        <ImageButton
            android:id="@+id/imageMuteIcon2"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@drawable/muted" />
    </GridLayout>
    <View style="@style/Divider"/>
</LinearLayout>

我必须以编程方式在Java中创建相同的设计。并在“图像按钮”中添加侦听器,以便我可以处理按钮。

1 个答案:

答案 0 :(得分:0)

我认为,您提到过AlertDialog 您可以尝试编写此方法:

public void entre () {

    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setTitle("Something");
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //what you need to do after click "OK"
        }

    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //what you need to do after click "Cancel"
        }
    });
    alert.show();
}

并将其添加到onClickListner中:

public void onButtonClick(View view) {
    entre ();
}

您将获得标题为“ Something”的弹出窗口,并带有两个按钮:“ Ok”和“ Cancel”