Android Custom view —- bottom sheet

第一次发博客。
现在准备学一学安卓的自定义组件,这方面一直不太行,现在把自己的学习过程发上来与大家共享。
感觉BottomSheet这个东西还是相对简单点的,今天就先写一个最简单的。

BottomSheet的定义是这样的这里写图片描述

首先在dimen中把我们需要的写出来

<dimen name="bottomsheetheight">48dp</dimen>
    <dimen name="bottom_sheet_padding_vertical">8dp</dimen>
    <dimen name="bottom_sheet_padding_horizontal">16dp</dimen>
    <dimen name="bottom_sheet_bitmap">24dp</dimen>
    <dimen name="bottom_sheet_margin_vertical">8dp</dimen>
    <dimen name="bottom_sheet_margin_horizontal">16dp</dimen>

在这里我使用了继承Dialog来实现BottomSheet,在开始之前,我们定义一下BottomSheet的style

<style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
    </style>
    <style name="MaterialDialogSheetAnimation">
        <item name="android:windowEnterAnimation">@anim/popup_show</item>
        <item name="android:windowExitAnimation">@anim/popup_hide</item>

如果不设置style,大家可以自己看看结果会变成什么样,popup_show和popup_hide大家可以自行查看源码,我就不贴出来了

最后自定义一个Dialog就行了,

public class SimpleBottomSheet extends Dialog{
    LinearLayoutCompat linearLayoutCompat;
    LinearLayoutCompat.LayoutParams params;
    Context context;
    GestureDetector detector;
    float temp;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("AAA",event.toString());
        if(event.getAction()== MotionEvent.ACTION_DOWN){
            temp=event.getY();
        }else if(event.getAction()== MotionEvent.ACTION_UP){
            if(event.getY()-temp>100){
                hide();
                //hide dialog when the change of y is larger than 100 between DOWN and UP
            }
        }
        return true;
    }
    public SimpleBottomSheet(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(linearLayoutCompat);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }
    void init(Context context){
        this.context=context;
        linearLayoutCompat=new LinearLayoutCompat(context);
        params=new ActionMenuView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayoutCompat.setPadding(
                context.getResources().getDimensionPixelOffset(R.dimen.bottom_sheet_margin_horizontal),
                context.getResources().getDimensionPixelOffset(R.dimen.bottom_sheet_margin_vertical),
                context.getResources().getDimensionPixelOffset(R.dimen.bottom_sheet_margin_horizontal),
                context.getResources().getDimensionPixelOffset(R.dimen.bottom_sheet_padding_vertical)
        );
        linearLayoutCompat.setOrientation(LinearLayoutCompat.VERTICAL);
        linearLayoutCompat.setLayoutParams(params);
    }
    public void add(Bitmap bitmap,String list){
        LinearLayoutCompat compat=new LinearLayoutCompat(context);
        compat.setOrientation(LinearLayoutCompat.HORIZONTAL);
        Resources resources=context.getResources();
        compat.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                resources.getDimensionPixelOffset(R.dimen.bottomsheetheight)));
        TextView textView=new TextView(context);
        textView.setText(list);
        textView.setPadding(resources.getDimensionPixelOffset(R.dimen.bottom_sheet_padding_horizontal),
                0, 0, 0
        );
        textView.setTextSize(20);
        ImageView b=new ImageView(context);
        b.setImageBitmap(bitmap);
        b.setPadding(0, 0, resources.getDimensionPixelOffset(R.dimen.bottom_sheet_padding_horizontal), 0);
        b.setForegroundGravity(Gravity.CENTER);
        compat.setGravity(Gravity.CENTER_VERTICAL);
        compat.addView(b);
        compat.addView(textView);
        linearLayoutCompat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        linearLayoutCompat.addView(compat);
    }
}

最后的效果截图
这里写图片描述

大家可以到我的github上查看源码 https://github.com/jifaxu/BottomSheet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值