Android----------PopWindow

1.PopWindow的效果为:

 2.首先点击 PopWindow练习 进入   

 3. 点击最喜爱的科目 弹出 列表界面 

4.  再次点击消失  

 5.当点击数学时  弹出 

 6.以此类推 语文也一样。

JAVA:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class PopupActivity extends AppCompatActivity {

      private Button btn_popup;
      private PopupWindow mpopup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);
        btn_popup=findViewById(R.id.btn_popup);
        btn_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View popview =getLayoutInflater().inflate(R.layout.layout_pop,null);
                TextView textView1=popview.findViewById(R.id.shuxue);
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mpopup.dismiss();
                        Toast.makeText(PopupActivity.this,"数学课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                TextView textView2=popview.findViewById(R.id.yuwen);
                textView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(PopupActivity.this,"语文课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                TextView textView3=popview.findViewById(R.id.yingyu);
                textView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(PopupActivity.this,"英语课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                 mpopup=new PopupWindow(popview,btn_popup.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                 mpopup.setOutsideTouchable(true);
                 mpopup.setFocusable(true);
                 mpopup.showAsDropDown(btn_popup);
            }
        });
    }
}

  XML1:

<?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"
    android:padding="10dp">

    <Button
        android:id="@+id/btn_popup"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="最喜爱的科目"
        android:layout_gravity="center"
        android:layout_marginTop="200dp"/>

</LinearLayout>

  XML2:

<?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"
    android:padding="15dp"
    android:background="@drawable/bg_dropdown">
    <TextView
        android:id="@+id/shuxue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="数学"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#A9A9A9"/>
    <TextView
        android:id="@+id/yuwen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="语文"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#A9A9A9"/>
    <TextView
        android:id="@+id/yingyu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="英语"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>

</LinearLayout>

  

原文地址:https://www.cnblogs.com/skyfail/p/13942439.html