日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

浅谈PopupWindow弹出菜单

發(fā)布時(shí)間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈PopupWindow弹出菜单 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

實(shí)現(xiàn)將一個(gè)View顯示在某一位置,而且是浮于當(dāng)前窗口

?

首先要有一個(gè)要顯示的view的布局,可以是任意View,包括ViewGroup

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="#d3d3d3" 6 android:gravity="center_horizontal" 7 android:orientation="vertical" > 8 9 10 <TextView 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:gravity="center" 14 android:id="@+id/textView" 15 android:text="文本內(nèi)容" 16 /> 17 </LinearLayout>

?

然后主界面布局文件

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal" 11 > 12 <!-- 區(qū)域 --> 13 <Button 14 android:layout_width="0dp" 15 android:layout_weight="1" 16 android:gravity="center" 17 android:background="#0000" 18 android:layout_height="wrap_content" 19 android:text="區(qū)域" 20 android:drawableRight="@drawable/title_bar_arrow_nor" 21 android:id="@+id/btn_1" 22 /> 23 <!-- 類別 --> 24 <Button 25 android:background="#0000" 26 android:layout_width="0dp" 27 android:layout_weight="1" 28 android:layout_height="wrap_content" 29 android:text="類別" 30 android:drawableRight="@drawable/title_bar_arrow_nor" 31 android:id="@+id/btn_2" 32 /> 33 34 <!-- 價(jià)格 --> 35 <Button 36 android:background="#0000" 37 android:layout_width="0dp" 38 android:layout_weight="1" 39 android:layout_height="wrap_content" 40 android:text="價(jià)格" 41 android:drawableRight="@drawable/title_bar_arrow_nor" 42 android:id="@+id/btn_3" 43 /> 44 45 <!-- 更多 --> 46 <Button 47 android:background="#0000" 48 android:layout_width="0dp" 49 android:layout_weight="1" 50 android:layout_height="wrap_content" 51 android:text="更多" 52 android:drawableRight="@drawable/title_bar_arrow_nor" 53 android:id="@+id/btn_4" 54 /> 55 56 </LinearLayout> 57 58 </RelativeLayout>

?

主activity

1 package com.example.aaa; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.ViewGroup; 8 import android.widget.Button; 9 import android.widget.PopupWindow; 10 import android.widget.TextView; 11 12 public class MainActivity extends Activity implements OnClickListener { 13 14 private PopupWindow pop; 15 16 private Button btn1; 17 private Button btn2; 18 private Button btn3; 19 private Button btn4; 20 21 private TextView text; 22 23 View view; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 initView(); 30 initPopWindow(); 31 32 } 33 34 private void initView() { 35 // TODO Auto-generated method stub 36 btn1 = (Button) findViewById(R.id.btn_1); 37 btn2 = (Button) findViewById(R.id.btn_2); 38 btn3 = (Button) findViewById(R.id.btn_3); 39 btn4 = (Button) findViewById(R.id.btn_4); 40 btn1.setOnClickListener(this); 41 btn2.setOnClickListener(this); 42 btn3.setOnClickListener(this); 43 btn4.setOnClickListener(this); 44 } 45 46 47 private void initPopWindow() { 48 // TODO Auto-generated method stub 49 //根據(jù)layout創(chuàng)建彈出界面 50 view = this.getLayoutInflater().inflate(R.layout.popup_window, null); 51 pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, 52 ViewGroup.LayoutParams.WRAP_CONTENT); 53 text = (TextView) view.findViewById(R.id.textView); 54 55 pop.setOutsideTouchable(true); 56 view.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 // TODO Auto-generated method stub 60 pop.dismiss(); 61 } 62 }); 63 64 } 65 66 @Override 67 public void onClick(View v) { 68 // TODO Auto-generated method stub 69 switch (v.getId()) { 70 case R.id.btn_1: 71 text.setText("區(qū)域"); 72 if(pop.isShowing()) 73 pop.dismiss(); 74 else 75 pop.showAsDropDown(v); 76 break; 77 case R.id.btn_2: 78 text.setText("類別"); 79 if(pop.isShowing()) 80 pop.dismiss(); 81 else 82 pop.showAsDropDown(v); 83 break; 84 case R.id.btn_3: 85 text.setText("價(jià)格"); 86 if(pop.isShowing()) 87 pop.dismiss(); 88 else 89 pop.showAsDropDown(v); 90 break; 91 case R.id.btn_4: 92 text.setText("更多"); 93 if(pop.isShowing()) 94 pop.dismiss(); 95 else 96 pop.showAsDropDown(v); 97 break; 98 99 default: 100 break; 101 } 102 } 103 104 }

?

?

?

出現(xiàn)位置的幾個(gè)方法

showAsDropDown(View anchor):相對(duì)某個(gè)控件的位置(正左下方),無偏移 showAsDropDown(View anchor, int xoff, int yoff):相對(duì)某個(gè)控件的位置,有偏移 showAtLocation(View parent, int gravity, int x, int y):相對(duì)于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移

?

效果圖:

總結(jié)

以上是生活随笔為你收集整理的浅谈PopupWindow弹出菜单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。