Android Material Design之BottomSheetDialog

 

顾名思义,BottomSheetDialog指从底部弹出的对话框。跟PopupWindow的效果类似。

先看下效果图:

Android Material Design之BottomSheetDialog_第1张图片

1.gif

可以看出效果,跟PopupWindow效果,差不多,但是PopupWindow要实现背景透明效果,必须使用代码进行设置,但是BottomSheetDialog不用,它的默认效果就是这样。

使用方法:

1、添加依赖:

compile 'com.android.support:design:24.1.1'

2、activity布局




3、dialog布局



    
    
    
    
    


4、Activity

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class BottomSheetActivity extends AppCompatActivity {

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

    }

    public void showDialog(View view){
        final BottomSheetDialog dialog=new BottomSheetDialog(BottomSheetActivity.this);
         View dialogView= LayoutInflater.from(BottomSheetActivity.this)
                .inflate(R.layout.layout_bottom,null);
        TextView tvTakePhoto= (TextView) dialogView.findViewById(R.id.tv_take_photo);
        TextView tvPhotoAlbum= (TextView) dialogView.findViewById(R.id.tv_photo_album);
        TextView tvCancel= (TextView) dialogView.findViewById(R.id.tv_cancel);

        tvTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BottomSheetActivity.this,"拍照",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        tvPhotoAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BottomSheetActivity.this,"拍照",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setContentView(dialogView);
        dialog.show();
    }

}

使用起来非常简单,几行代码就ok

BottomSheetDialog dialog=new BottomSheetDialog(BottomSheetActivity.this);
View dialogView= LayoutInflater.from(BottomSheetActivity.this).inflate(R.layout.layout_bottom,null);
dialog.setContentView(dialogView);
dialog.show();

当弹出的layout是一个ListView的时候,当ListView的item比较多时,弹出对话框时只会显示几个item,向上拖动时,才会显示全部item

Android Material Design之BottomSheetDialog_第2张图片

2.gif

layout布局文件





Activity核心代码

 public void showDialog(View view){
        final BottomSheetDialog dialog=new BottomSheetDialog(BottomSheetActivity.this);
        View dialogView= LayoutInflater.from(BottomSheetActivity.this)
                .inflate(R.layout.list_bottom,null);
        ListView listView= (ListView) dialogView.findViewById(R.id.listview);
        String[] array=new String[]{"item-1","item-2","item-3","item-4","item-5","item-6","item-7","item-8","item-9"};
        ArrayAdapter adapter=new ArrayAdapter(BottomSheetActivity.this,android.R.layout.simple_list_item_1,array);
        listView.setAdapter(adapter);

        dialog.setContentView(dialogView);
        dialog.show();
    }

转载地址:https://www.jianshu.com/p/de5f9cdfba56

你可能感兴趣的:(Android Material Design之BottomSheetDialog)