亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年07月22日 16:27:48   作者:yxx_bk  
這篇文章主要為大家詳細(xì)介紹了Android Popupwindow彈出窗口的簡單使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Popupwindow彈出窗口的具體代碼,供大家參考,具體內(nè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的背景,無論是點(diǎn)擊外部區(qū)域還是Back鍵都無法dismiss彈框
    // 我覺得這里是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>

效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論