如何更改bottomSheet打开/显示样式,如默认是从下到上我想要从上到下

时间:2016-07-25 07:23:12

标签: android

我想从上到下显示底部工作表。默认BottomSheet打开从下到上,但我想要打开底部工作表标题/工具栏/ ActionBar

我在Google上搜索但无法找到解决方案

这是我的代码

activity_main.xml中

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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"
    android:id="@+id/cordinate"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.bttomsheettoptobottom.MainActivity">

    <Button
        android:id="@+id/btOpenBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Show Bottom Sheet"/>

    <LinearLayout
        android:id="@+id/llBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorPrimary"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        android:orientation="vertical">

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

MainActivity.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Button btOpenBottomSheet;
    private CoordinatorLayout coordinatorLayout;


    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            switch (view.getId()){

                case R.id.btOpenBottomSheet:

                    showBottomSheetDialog();

                    break;
            }

        }

    };


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

        init();
    }

    private void init() {

        btOpenBottomSheet = (Button)findViewById(R.id.btOpenBottomSheet);
        btOpenBottomSheet.setOnClickListener(mOnClickListener);
    }

    private void showBottomSheetDialog() {


        CoordinatorLayout coordinatorLayout = (CoordinatorLayout)findViewById(R.id.cordinate);

        View bottomSheet = coordinatorLayout.findViewById(R.id.llBottomSheet);
        BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                // React to state change
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                // React to dragging events
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

创建一个自定义的TopsheetBehaviour类,如图here所示,并使工作表行为为

If you are using authentication created by the 
composer require laravel/ui
php artisan ui vue --auth

You can add to

// app.blade.php or whatever you have maybe master.blade.php
 @if (Auth::check()) 
         <meta name="user_id" content="{{ Auth::user()->id }}" />
 @endif 

Then in the app.js add
Vue.prototype.$userId = document.querySelector("meta[name='user_id']").getAttribute('content');


Now if you want to get the logged in  user id and pass it to a hidden input field the first declare it as 

<script>

export default {
        props: ['app'],
        data()
        {
            return{
                user_id: this.$userId,
            }
        },

    }
</script>

  // Finally in your MyVueComponent.vue
<input type="hidden" name="user_id" v-model="user_id">

在我的情况下有效。希望对您有帮助!