Android studio實現(xiàn)PopupWindow彈出框效果
本文實例為大家分享了Android studio實現(xiàn)PopupWindow彈出框的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)步驟:
第一步:自定義.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:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="提示" android:background="@drawable/click" android:textSize="28sp"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:text="你真的要離開嗎" android:textColor="#ff0400" android:background="@drawable/background" android:textSize="28sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="50sp" android:orientation="horizontal" android:background="@drawable/bb" android:gravity="center"> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="殘忍離開" android:textSize="24sp"/> </LinearLayout> </LinearLayout>
第二步:獲取layoutInflater對象
第三步:調(diào)用inflater()方法獲取View對象
第四步:創(chuàng)建PopupWindow對象
第五步:調(diào)用PopupWindow的showAsDropDown或者showAsLocation方法顯示對話框窗口
下拉式
默認(rèn)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"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="對話框" android:onClick="onclick"/> </LinearLayout>
java:
package com.example.catalogin; import android.app.Dialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.PopupWindow; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main2); } PopupWindow popupWindow; public void onclick(View v1){ LayoutInflater inflater=LayoutInflater.from( this ); View myview=inflater.inflate(R.layout.catalogin,null);//引用自定義布局 popupWindow=new PopupWindow( myview,600,500 );//后面是像素大小 myview.findViewById(R.id.but).setOnClickListener( new View.OnClickListener() {//獲取布局里面按鈕 @Override public void onClick(View v) { popupWindow.dismiss();//點擊按鈕對話框消失 Toast.makeText( Main2Activity.this, "點擊了殘忍離開", Toast.LENGTH_SHORT ).show(); } } ); popupWindow.showAsDropDown(v1); } }
彈出式
代碼popupWindow.showAsDropDown(v1);
換成popupWindow.showAtLocation( v1,Gravity.CENTER,0,0 );//偏移量
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義帶加載動畫效果的環(huán)狀進(jìn)度條
這篇文章主要介紹了Android自定義帶加載動畫效果的環(huán)狀進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android仿微信@好友功能 輸入@跳轉(zhuǎn)、刪除整塊
這篇文章主要介紹了Android仿微信@好友功能 輸入@跳轉(zhuǎn)、刪除整塊,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-10-10Android自定義水平進(jìn)度條的圓角進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義水平進(jìn)度條的圓角進(jìn)度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android Studio 升級到3.0后輸入法中文狀態(tài)下無法選詞的終極解決方案
這篇文章主要介紹了 AndroidStudio 升級到3.0后輸入法中文狀態(tài)下無法選詞的解決方案,需要的朋友可以參考下2017-11-11AndroidStudio實現(xiàn)微信界面設(shè)計
這篇文章帶你通過Androidstudio來實現(xiàn)微信的基礎(chǔ)界面,微信的界面主要包含了主頁、通訊錄、發(fā)現(xiàn)以及我的賬號功能區(qū),下文包含了整個開發(fā)過程,以及解決該問題的過程及思路并提供了源碼2021-10-10