Android中底部滑入的控件BottomSheetLayout( 第三方 )的使用

最近在github上面看到一个底部滑入控件,觉得写得挺好,所以把用法总结一下:
https://github.com/Flipboard/bottomsheet
效果图:
这里写图片描述
第1步:
在build.gradle中引用
compile ‘com.flipboard:bottomsheet-core:1.5.1’
第2步
在activity的布局activity_main.xml中

<com.flipboard.bottomsheet.BottomSheetLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <Button
            android:id="@+id/btnTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="测试"
            />
    </LinearLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

第3步:BottomSheetLayout控件中展示的布局layout_bottom_sheet.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="wrap_content"

    android:orientation="vertical">
    <TextView
        android:id="@+id/tvTest"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="内容1"
        android:textSize="20dp"
        android:gravity="center"
        android:background="#eeeeee"
        android:layout_marginTop="1dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="内容2"
        android:textSize="20dp"
        android:gravity="center"
        android:background="#eeeeee"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="内容3"
        android:textSize="20dp"
        android:gravity="center"
        android:background="#eeeeee"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="内容4"
        android:textSize="20dp"
        android:gravity="center"
        android:background="#eeeeee"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="内容5"
        android:textSize="20dp"
        android:gravity="center"
        android:background="#eeeeee"
        android:layout_marginTop="2dp"
        />

</LinearLayout>

第4步MainActivity中使用

package com.zhh.bottomsheetlayout;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flipboard.bottomsheet.BottomSheetLayout;

public class MainActivity extends Activity {
//  第三方控件
    private BottomSheetLayout bottomSheetLayout;
//  弹出框中的子布局
    private View bottomSheet;
//  点击按钮
    private TextView btnTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//      初始化控件
        bottomSheetLayout = findViewById(R.id.bottomSheetLayout);
        btnTest = findViewById(R.id.btnTest);
//      点击事件
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheet();
            }
        });
    }

    /**
     * 从底部滑入
     */
    private void showBottomSheet() {

//          创建要弹出的布局
            bottomSheet = createBottomSheetView();

//          判断打开关闭
        if (bottomSheetLayout.isSheetShowing()) {
            bottomSheetLayout.dismissSheet();
        } else {
//          弹出布局
            bottomSheetLayout.showWithSheetView(bottomSheet);
        }
    }

    /**
     * 从底部弹出的子布局
     * @return
     */
    private View createBottomSheetView() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_bottom_sheet, (ViewGroup) getWindow().getDecorView(), false);
        TextView tvTest = (TextView) view.findViewById(R.id.tvTest);
        return view;
    }
}

以上是所有代码:
源码下载:
https://download.csdn.net/download/zhaihaohao1/10534395

猜你喜欢

转载自blog.csdn.net/zhaihaohao1/article/details/80997496