android--popwindow

java代码

 1 package com.example.myapplication;
 2   
 3 import androidx.appcompat.app.AppCompatActivity;
 4   
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.Button;
 9 import android.widget.PopupWindow;
10 import android.widget.TextView;
11 import android.widget.Toast;
12   
13 public class PopupActivity extends AppCompatActivity {
14   
15       private Button btn_popup;
16       private PopupWindow mpopup;
17   
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_popup);
22         btn_popup=findViewById(R.id.btn_popup);
23         btn_popup.setOnClickListener(new View.OnClickListener() {
24             @Override
25             public void onClick(View view) {
26                 View popview =getLayoutInflater().inflate(R.layout.layout_pop,null);
27                 TextView textView1=popview.findViewById(R.id.shuxue);
28                 textView1.setOnClickListener(new View.OnClickListener() {
29                     @Override
30                     public void onClick(View view) {
31                         mpopup.dismiss();
32                         Toast.makeText(PopupActivity.this,"数学课很好玩",Toast.LENGTH_SHORT).show();
33                     }
34                 });
35                 TextView textView2=popview.findViewById(R.id.yuwen);
36                 textView2.setOnClickListener(new View.OnClickListener() {
37                     @Override
38                     public void onClick(View view) {
39                         Toast.makeText(PopupActivity.this,"语文课很好玩",Toast.LENGTH_SHORT).show();
40                     }
41                 });
42                 TextView textView3=popview.findViewById(R.id.yingyu);
43                 textView3.setOnClickListener(new View.OnClickListener() {
44                     @Override
45                     public void onClick(View view) {
46                         Toast.makeText(PopupActivity.this,"英语课很好玩",Toast.LENGTH_SHORT).show();
47                     }
48                 });
49                  mpopup=new PopupWindow(popview,btn_popup.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
50                  mpopup.setOutsideTouchable(true);
51                  mpopup.setFocusable(true);
52                  mpopup.showAsDropDown(btn_popup);
53             }
54         });
55     }
56 }

xml1代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:padding="10dp">
 7   
 8     <Button
 9         android:id="@+id/btn_popup"
10         android:layout_width="200dp"
11         android:layout_height="wrap_content"
12         android:text="最喜爱的科目"
13         android:layout_gravity="center"
14         android:layout_marginTop="200dp"/>
15   
16 </LinearLayout>

xml2代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:padding="15dp"
 8     android:background="@drawable/bg_dropdown">
 9     <TextView
10         android:id="@+id/shuxue"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:textSize="20sp"
14         android:textColor="#000000"
15         android:text="数学"
16         android:gravity="center"
17         android:layout_marginTop="8dp"
18         android:layout_marginBottom="8dp"/>
19     <View
20         android:layout_width="match_parent"
21         android:layout_height="0.5dp"
22         android:background="#A9A9A9"/>
23     <TextView
24         android:id="@+id/yuwen"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:textSize="20sp"
28         android:textColor="#000000"
29         android:text="语文"
30         android:gravity="center"
31         android:layout_marginTop="8dp"
32         android:layout_marginBottom="8dp"/>
33     <View
34         android:layout_width="match_parent"
35         android:layout_height="0.5dp"
36         android:background="#A9A9A9"/>
37     <TextView
38         android:id="@+id/yingyu"
39         android:layout_width="match_parent"
40         android:layout_height="wrap_content"
41         android:textSize="20sp"
42         android:textColor="#000000"
43         android:text="英语"
44         android:gravity="center"
45         android:layout_marginTop="8dp"
46         android:layout_marginBottom="8dp"/>
47   
48 </LinearLayout>

 

posted @ 2020-12-20 17:12  刘显栋  阅读(101)  评论(0编辑  收藏  举报