Android studio弹出框效果 Android studio实现PopupWindow弹出框效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android studio弹出框效果 Android studio实现PopupWindow弹出框效果

Be your bubble   2021-10-20 我要评论
想了解Android studio实现PopupWindow弹出框效果的相关内容吗,Be your bubble在本文为您仔细讲解Android studio弹出框效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,studio,弹出框,下面大家一起来学习吧。


实现步骤:

第一步:自定义.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对象
第四步:创建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 );//偏移量

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们