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

Android studio實現(xiàn)PopupWindow彈出框效果

 更新時間:2021年10月20日 11:52:59   作者:Be your bubble  
這篇文章主要為大家詳細(xì)介紹了Android studio實現(xiàn)PopupWindow彈出框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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)文章

最新評論