亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)之PopupWindow實現(xiàn)彈窗效果

 更新時間:2022年09月19日 11:37:09   作者:ShadyPi  
這篇文章主要為大家詳細介紹了Android開發(fā)之PopupWindow實現(xiàn)彈窗效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android開發(fā)之PopupWindow實現(xiàn)彈窗的具體代碼,供大家參考,具體內(nèi)容如下

基本框架

activity_main.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">

? ? <Button
? ? ? ? android:text="Popup"
? ? ? ? android:onClick="Popup"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>


</LinearLayout>

再編寫一個Layout資源文件popup_view.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:background="@mipmap/ic_launcher"
? ? android:orientation="vertical">

? ? <Button
? ? ? ? android:id="@+id/btn1"
? ? ? ? android:text="Button1"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>

? ? <Button
? ? ? ? android:id="@+id/btn2"
? ? ? ? android:text="Button2"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>


</LinearLayout>

MainActivity.java中編寫按鈕的點擊事件,同樣用View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null);將上面編寫的Layout資源轉(zhuǎn)換成View,之后就可以新建一個彈窗并讓其彈出。

package com.example.mypopupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }

? ? public void Popup(View view) {
? ? ? ? View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null);
? ? ? ? PopupWindow window=new PopupWindow(popup_view, ViewGroup.LayoutParams.WRAP_CONTENT
? ? ? ? ? ? ? ? ,ViewGroup.LayoutParams.WRAP_CONTENT);
? ? ? ? window.showAsDropDown(view);
? ? }
}

點擊按鈕就會得到彈窗:

常用方法

setContentView(View contentView) 設置彈窗的View
showAsDropDown(View anchor) 彈窗的位置在控件的下方
showAsDropDown(View anchor,int xoff,int yoff) 相對某個控件的位置,有偏移
setFocusable(boolean focusable) 設置是否獲取焦點
setBackgroundDrawable(Drawable background) 設置背景
dismiss() 關閉窗口
setAnimationStyle(int animationStyle) 設置加載動畫(在學習了動畫后再深入)
setTouchable(boolean touchable) 設置觸摸使能
setOutsideTouchable(boolean touchable) 設置彈窗外面的觸摸使能

實例

將focusable設置為true,就可以通過點擊彈窗以外的地方關閉彈窗。

使用showAsDropDown(View anchor,int xoff,int yoff)就可以讓彈窗有偏移:

使用window.setBackgroundDrawable(getResources().getDrawable(R.drawable.image));設置彈窗的圖片背景:

對于彈窗中的兩個按鈕也可以設置監(jiān)聽,從而實現(xiàn)一些點擊事件,在點擊事件的結(jié)尾可以添加dismiss()函數(shù)使得點擊后彈窗關閉。

package com.example.mypopupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void Popup(View view) {
? ? ? ? View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null);
? ? ? ? Button btn1=popup_view.findViewById(R.id.btn1);
? ? ? ? Button btn2=popup_view.findViewById(R.id.btn2);

? ? ? ? PopupWindow window=new PopupWindow(popup_view, ViewGroup.LayoutParams.WRAP_CONTENT
? ? ? ? ? ? ? ? ,ViewGroup.LayoutParams.WRAP_CONTENT,true);
? ? ? ? window.setBackgroundDrawable(getResources().getDrawable(R.drawable.image));
? ? ? ? window.showAsDropDown(view);

? ? ? ? btn1.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("ShadyPi","btn1");
? ? ? ? ? ? ? ? window.dismiss();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? btn2.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("ShadyPi","btn2");
? ? ? ? ? ? ? ? window.dismiss();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論