Android开发实现popupWindow弹出窗口自定义布局与位置控制方法

所属分类: 软件编程 / Android 阅读数: 65
收藏 0 赞 0 分享

本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下:

布局文件:

主布局文件:activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.bige.MainActivity"
  android:orientation="vertical">
  <LinearLayout
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:textSize="20dp"
      android:paddingTop="10dp"
      android:gravity="left"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="3"
      android:text="富乃宝山"
      android:background="#AAAAAA"/>
    <Button
      android:layout_marginRight="5dp"
      android:layout_marginLeft="5dp"
      android:background="#AAAAAA"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="0点"/>
    <Button
      android:id="@+id/num1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:textColor="@android:color/white"
      android:background="#000000"
      android:text="1"/>
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ビール"
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="FD"
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"
      />
    <Button
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="焼酎"
      android:background="#6FA5DB"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="日本酒"
      android:background="#A4A4A4"/>
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ソフト "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="サワー "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"
      />
    <Button
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="カクテル "
      android:background="#A4A4A4"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ワイン "
      android:background="#A4A4A4"/>
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="果実酒 "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ウイスキー "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"
      />
    <Button
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"/>
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="会席進行 "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="パック進行 "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"
      />
    <Button
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="単品 "
      android:background="#A4A4A4"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"/>
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_marginTop="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ソフト "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="サワー "
      android:background="#A4A4A4"
      android:layout_marginRight="3dp"
      />
    <Button
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="カクテル "
      android:background="#A4A4A4"/>
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="ワイン "
      android:background="#A4A4A4"/>
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"
      android:text="ロック " />
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"
      android:text="水割り " />
    <Button
      android:layout_marginRight="3dp"
      android:layout_marginLeft="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"
      android:text="お湯割り " />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"
      android:text="ソーダ割 " />
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_marginLeft="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_marginLeft="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_marginLeft="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4"
      android:text=" " />
    <Button
      android:layout_marginRight="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_marginRight="3dp"
      android:layout_marginLeft="3dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="#A4A4A4" />
  </LinearLayout>
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="0dp"
    android:background="#5399D9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginLeft="15dp"
      android:layout_marginRight="3dp"
      android:background="#FFC727"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="AC"/>
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginRight="3dp"
      android:background="#6DB040"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="NG"/>
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginRight="3dp"
      android:background="#6DB040"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="後"/>
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginRight="3dp"
      android:background="#6DB040"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="急"/>
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginRight="3dp"
      android:background="#6DB040"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="▲"/>
    <Button
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_marginRight="15dp"
      android:background="#FFC727"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="確認"/>
  </LinearLayout>
</LinearLayout>

弹出布局设置:

number:

<?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:orientation="vertical"
  android:id="@+id/number"
  android:background="#FCFCFC">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
      android:gravity="right"
      android:background="@drawable/edittext"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:hint="0"
      android:textSize="35dp"
      />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="7" />
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="8" />
    <Button
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="9" />
  </LinearLayout>
  <LinearLayout
    android:layout_marginTop="1dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="4" />
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="5" />
    <Button
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="6" />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="1" />
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="2" />
    <Button
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="3" />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="0" />
    <Button
      android:layout_marginRight="1dp"
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="→" />
    <Button
      android:background="@drawable/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="C" />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="@drawable/button"
      android:text="戻る" />
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="@drawable/button"
      android:text="OK" />
  </LinearLayout>
</LinearLayout>

MainActivity布局:

package com.example.bige;
import android.support.v4.widget.PopupWindowCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
  Button mbtn;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    View root = this.getLayoutInflater().inflate(R.layout.number,null);
    //设置弹出布局自适应大小
    final PopupWindow popup = new PopupWindow(root, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    mbtn = (Button) findViewById(R.id.num1);
    mbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        // popup.isShowing();
        Toast.makeText(MainActivity.this, "hh", Toast.LENGTH_SHORT).show();
         //控制pupup弹出位置在父布局的中间显示
         //四个参数分别表示,要参考的控件view,相对位置,后边两个参数int x,int y表示偏移
        popup.showAtLocation(view, Gravity.CENTER,0,0);
      }
    });
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android窗口相关操作技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多