Android自定义dialog从屏幕底部弹出并且充满屏幕宽度

效果如下

Android自定义dialog从屏幕底部弹出并且充满屏幕宽度_第1张图片

1.首先我们先来定义一个dialog的布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:id="@+id/bt_weixin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="微信"
        android:layout_weight="1"
        android:background="@color/material_blue_grey_800"/>
    <Button
        android:id="@+id/bt_weibo"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#ef3254"
        android:text="微博"/>
    <Button
        android:id="@+id/bt_pengyouquan"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#ef6d03"
        android:text="朋友圈"/>

LinearLayout>

2.activity_main.xml非常简单,只有一个button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/share"
        android:text="分享" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


RelativeLayout>

3.接着我们要在res/values/styles.xml下定义dialog的样式以及dialog弹出时的动画

-- 弹出框动画 由下至上 -->
    

 -- 对话框样式 -->
    

4.接着我们在res下新建一个anim文件夹,在res/values/anim下新建两个xml文件
Android自定义dialog从屏幕底部弹出并且充满屏幕宽度_第2张图片
dialog_enter.xml和dialog_exit.xml如下


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="100%"
        android:duration="600">translate>
set>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:toYDelta="100%"
        android:duration="600">translate>
set>

5.最后是MainActivity.Java

public class MainActivity extends Activity {
    private Button btShare;
    private Context mContext;
    private Button btWeixin;
    private Button btWeibo;
    private Button btPengyouquan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        btShare = (Button) findViewById(R.id.share);
        btShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(mContext, R.style.Theme_Light_Dialog);
                View dialogView = LayoutInflater.from(mContext).inflate(R.layout.my_dialog,null);
                //获得dialog的window窗口
                Window window = dialog.getWindow();
                //设置dialog在屏幕底部
                window.setGravity(Gravity.BOTTOM);
                //设置dialog弹出时的动画效果,从屏幕底部向上弹出
                window.setWindowAnimations(R.style.dialogStyle);
                window.getDecorView().setPadding(0, 0, 0, 0);
                //获得window窗口的属性
                android.view.WindowManager.LayoutParams lp = window.getAttributes();
                //设置窗口宽度为充满全屏
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                //设置窗口高度为包裹内容
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                //将设置好的属性set回去
                window.setAttributes(lp);
                //将自定义布局加载到dialog上
                dialog.setContentView(dialogView);
                btWeixin = (Button) dialogView.findViewById(R.id.bt_weixin);
                btWeibo = (Button) dialogView.findViewById(R.id.bt_weibo);
                btPengyouquan = (Button) dialogView.findViewById(R.id.bt_pengyouquan);
                dialog.show();
                btWeixin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"分享到微信",Toast.LENGTH_SHORT).show();
                    }
                });
                btWeibo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"分享到微博",Toast.LENGTH_SHORT).show();
                    }
                });
                btPengyouquan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"分享到朋友圈",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }


}

你可能感兴趣的:(Android)