Android開發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)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點(diǎn)"/> <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="會(huì)席進(jìn)行 " android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="パック進(jìn)行 " 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="確認(rèn)"/> </LinearLayout> </LinearLayout>
彈出布局設(shè)置:
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); //設(shè)置彈出布局自適應(yīng)大小 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彈出位置在父布局的中間顯示 //四個(gè)參數(shù)分別表示,要參考的控件view,相對(duì)位置,后邊兩個(gè)參數(shù)int x,int y表示偏移 popup.showAtLocation(view, Gravity.CENTER,0,0); } }); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android窗口相關(guān)操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開發(fā)之PopupWindow實(shí)現(xiàn)彈窗效果
- Android彈窗ListPopupWindow的簡單應(yīng)用詳解
- Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能
- Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
- Android Popupwindow彈出窗口的簡單使用方法
- Android編程實(shí)現(xiàn)的自定義彈窗(PopupWindow)功能示例
- Android自定義彈出窗口PopupWindow使用技巧
- Android控件PopupWindow模仿ios底部彈窗
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- Android中PopupWindow彈出式窗口使用方法詳解
相關(guān)文章
Android實(shí)現(xiàn)便于批量操作可多選的圖片ListView實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)便于批量操作可多選的圖片ListView功能實(shí)現(xiàn)方法,涉及ListView針對(duì)多圖操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08Android開發(fā)中解析xml文件XmlUtils工具類與用法示例
這篇文章主要介紹了Android開發(fā)中解析xml文件XmlUtils工具類與用法,結(jié)合實(shí)例形式分析了Android開發(fā)中解析xml文件工具類定義與相關(guān)使用技巧,需要的朋友可以參考下2018-01-01詳解android使用ItemDecoration 懸浮導(dǎo)航欄效果
本篇文章主要介紹了Android 最流行的吸頂效果的實(shí)現(xiàn)及代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Android實(shí)現(xiàn)簡單的popupwindow提示框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單的popupwindow提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android ToggleButton 詳解及實(shí)例代碼
這篇文章主要介紹了Android ToggleButton 詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Arduino 數(shù)據(jù)類型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹
這篇文章主要介紹了Arduino 數(shù)據(jù)類型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11