溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android Popupwindow彈出窗口的簡(jiǎn)單使用方法

發(fā)布時(shí)間:2020-09-28 19:45:20 來(lái)源:腳本之家 閱讀:189 作者:yxx_bk 欄目:移動(dòng)開(kāi)發(fā)

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

代碼很簡(jiǎn)單,沒(méi)有和別的控件連用。布局自己隨意定義,我的這個(gè)是最基礎(chǔ)的,就直接上代碼啦!

在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) {

    // 一個(gè)自定義的布局,作為顯示的內(nèi)容
    View contentView =LayoutInflater.from(mContext).inflate(
        R.layout.popupwindow, null);
    // 設(shè)置按鈕的點(diǎn)擊事件
    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);

    // 如果不設(shè)置PopupWindow的背景,無(wú)論是點(diǎn)擊外部區(qū)域還是Back鍵都無(wú)法dismiss彈框
    // 我覺(jué)得這里是API的一個(gè)bug
    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));

    // 設(shè)置好參數(shù)之后再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彈出窗口的簡(jiǎn)單使用方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI