zoukankan      html  css  js  c++  java
  • PopupWindow的简单使用

    测试代码:

    package com.zzw.testpopuwindows;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.PopupWindow;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final Button button = (Button) findViewById(R.id.button);
    
            button.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                    View view = inflater.inflate(R.layout.item, null);
    
                    PopupWindow mPopupWindow = new PopupWindow(view, 500, 300);
    
                    //设置背景的目的是使setOutsideTouchable方法生效
                    mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                    mPopupWindow.setOutsideTouchable(true);//在外点击消失
                    // mPopupWindow.showAsDropDown(button);
                    mPopupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
                }
            });
        }
    
    }

    item.xml:

    <?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"
        android:background="@android:color/darker_gray"
        android:orientation="vertical" >
    
        <ImageView
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PopupWindow显示"
            android:textColor="@android:color/holo_red_light" />
    
    </LinearLayout>
  • 相关阅读:
    numpy用法介绍-未完待续
    GeoJSON相关操作
    awk日志分析
    awk获取外部变量
    Shell编程二
    Shell编程
    Linux监控平台搭建
    Linux集群架构
    Linux集群
    MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5045914.html
Copyright © 2011-2022 走看看