欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android PopUpWindow使用詳解

 更新時(shí)間:2021年10月18日 17:12:26   作者:青素i  
PopupWindow與AlertDialog最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認(rèn)顯示在屏幕最中間(當(dāng)然也可以通過設(shè)置WindowManager參數(shù)來改變位置)。而PopupWindow是可以指定顯示位置的,隨便哪個(gè)位置都可以,更加靈活

概述

最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認(rèn)顯示在屏幕最中間(當(dāng)然也可以通過設(shè)置WindowManager參數(shù)來改變位置)。而PopupWindow是可以指定顯示位置的,隨便哪個(gè)位置都可以,更加靈活。

在這里插入圖片描述

聲明

構(gòu)造方法

//一:
public PopupWindow (Context context)
//二:
public PopupWindow(View contentView)
//三:
public PopupWindow(View contentView, int width, int height)
//四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

一個(gè)窗口標(biāo)準(zhǔn)的PopupWindow應(yīng)該有三個(gè)參數(shù) 上下文 寬、高

顯示函數(shù)

//相對(duì)某個(gè)控件的位置(正左下方),無偏移
showAsDropDown(View anchor):
//相對(duì)某個(gè)控件的位置,有偏移;xoff表示x軸的偏移
showAsDropDown(View anchor, int xoff, int yoff):
//正中央Gravity.CENTER,下方Gravity.BOTTOM等
showAtLocation(View parent, int gravity, int x, int y):

這里有兩種顯示方式:
1、顯示在某個(gè)指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父視圖,顯示在父控件的某個(gè)位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
3、view可以是任意組件

正常聲明一個(gè)PopupWindow代碼

設(shè)置需要載入的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000"
        android:text="我是一個(gè)PopupWindow"
        />

</LinearLayout>

創(chuàng)建PopupWindow

  //將xml文件轉(zhuǎn)成view
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow,null);
        //創(chuàng)建popupWindow 
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        //載入布局
        popupWindow.setContentView(view);
        //設(shè)置寬高
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

設(shè)置顯示位置

      //設(shè)置顯示位置 正左下方無偏移

        popupWindow.showAsDropDown(mTvShow);

完整代碼

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    TextView mTvShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvShow = findViewById(R.id.tvshow);
        mTvShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myPopupWindow();
            }
        });
    }




    private  void myPopupWindow(){
        //將xml文件轉(zhuǎn)成view
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow,null);
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        popupWindow.setContentView(view);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //PopupWindow popupWindow = new PopupWindow(MainActivity.this,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);


        //進(jìn)行正常頁面設(shè)置




        //設(shè)置顯示位置 正左下方無偏移

        popupWindow.showAsDropDown(mTvShow);

        //popupwindow 點(diǎn)擊外圍頁面 不會(huì)自動(dòng)消失 因此需要手動(dòng)設(shè)置一個(gè)關(guān)閉
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });


    }
}

在這里插入圖片描述

到此這篇關(guān)于Android PopUpWindow使用詳解的文章就介紹到這了,更多相關(guān)Android PopUpWindow內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論