国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Popupwindow彈出窗口的簡單使用方法

2019-10-22 18:31:46
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android Popupwindow彈出窗口的具體代碼,供大家參考,具體內容如下

代碼很簡單,沒有和別的控件連用。布局自己隨意定義,我的這個是最基礎的,就直接上代碼啦!

在MainActivity里

import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends AppCompatActivity {  private Context mContext = null;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mContext = this;    Button button = (Button) findViewById(R.id.but);    button.setOnClickListener(newView.OnClickListener() {      @Override      public void onClick(View view) {        showPopupWindow(view);      }    });  }  private void showPopupWindow(View view) {    // 一個自定義的布局,作為顯示的內容    View contentView =LayoutInflater.from(mContext).inflate(        R.layout.popupwindow, null);    // 設置按鈕的點擊事件    Button button = (Button) contentView.findViewById(R.id.button );      button.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {            Toast.makeText(mContext, "button is pressed",                Toast.LENGTH_SHORT).show();          }    });    final PopupWindow popupWindow = new PopupWindow(contentView,  LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);    // 如果不設置PopupWindow的背景,無論是點擊外部區域還是Back鍵都無法dismiss彈框    // 我覺得這里是API的一個bug    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));    // 設置好參數之后再show    popupWindow.showAsDropDown(view);  }}

在主布局里

 <Button    android:id="@+id/but"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Hello World!" />

在popupwindow布局里

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#ffff00"><TextView  android:id="@+id/ttt"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textSize="20sp"  android:text="彈出窗口"/>  <Button    android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /></LinearLayout>

效果圖

Android,Popupwindow,彈出窗口

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂荣县| 嘉禾县| 平定县| 平山县| 乡城县| 花莲县| 巴南区| 进贤县| 隆尧县| 木兰县| 商水县| 璧山县| 平原县| 南漳县| 奉节县| 敖汉旗| 自贡市| 长武县| 安徽省| 武山县| 金塔县| 寿阳县| 封丘县| 五家渠市| 迁安市| 兰考县| 江川县| 万全县| 大方县| 重庆市| 黎城县| 隆子县| 新晃| 寿光市| 福清市| 若羌县| 绍兴市| 乐业县| 太保市| 鄂托克前旗| 通河县|