最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

基本框架

activity_main.xml中設(shè)置一個(gè)按鈕,用于喚出彈窗;

<?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>

再編寫一個(gè)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中編寫按鈕的點(diǎn)擊事件,同樣用View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null);將上面編寫的Layout資源轉(zhuǎn)換成View,之后就可以新建一個(gè)彈窗并讓其彈出。

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);
? ? }
}

點(diǎn)擊按鈕就會(huì)得到彈窗:

常用方法

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

實(shí)例

將focusable設(shè)置為true,就可以通過點(diǎn)擊彈窗以外的地方關(guān)閉彈窗。

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

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

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

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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

罗源县| 自治县| 普宁市| 句容市| 南丹县| 乐清市| 临邑县| 岳池县| 桓台县| 宣城市| 通州区| 淳化县| 晴隆县| 奉化市| 会宁县| 民县| 娱乐| 雅江县| 盘山县| 垫江县| 鹤岗市| 永仁县| 自治县| 阿尔山市| 铅山县| 澜沧| 巫溪县| 尚志市| 凤山县| 大化| 临安市| 汨罗市| 武安市| 黄浦区| 红安县| 澎湖县| 邵阳市| 建始县| 双流县| 大同市| 菏泽市|