Android studio實現(xiàn)PopupWindow彈出框效果
本文實例為大家分享了Android studio實現(xiàn)PopupWindow彈出框的具體代碼,供大家參考,具體內容如下
實現(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對象
第三步:調用inflater()方法獲取View對象
第四步:創(chuàng)建PopupWindow對象
第五步:調用PopupWindow的showAsDropDown或者showAsLocation方法顯示對話框窗口
下拉式
默認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 );//偏移量
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android Studio 升級到3.0后輸入法中文狀態(tài)下無法選詞的終極解決方案
這篇文章主要介紹了 AndroidStudio 升級到3.0后輸入法中文狀態(tài)下無法選詞的解決方案,需要的朋友可以參考下2017-11-11