国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)實現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法

2019-10-22 18:27:05
字體:
供稿:網(wǎng)友

本文實例講述了Android開發(fā)實現(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點"/>    <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彈出位置在父布局的中間顯示         //四個參數(shù)分別表示,要參考的控件view,相對位置,后邊兩個參數(shù)int x,int y表示偏移        popup.showAtLocation(view, Gravity.CENTER,0,0);      }    });  }}

 

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 延川县| 景德镇市| 凌云县| 三江| 北安市| 灵川县| 若尔盖县| 兴宁市| 汪清县| 新建县| 沐川县| 印江| 安化县| 略阳县| 子长县| 甘孜| 柞水县| 无棣县| 奉贤区| 盈江县| 崇阳县| 宜丰县| 龙海市| 大邑县| 绍兴县| 苍南县| 台东市| 柳河县| 茂名市| 枣阳市| 潍坊市| 栾川县| 鄂尔多斯市| 府谷县| 昌江| 嘉善县| 涡阳县| 壤塘县| 肃宁县| 马鞍山市| 罗定市|