我想实现点击FloatingActionButton弹出菜单后,点击FloatingActionButton以外的view关闭FloatingActionButton弹出的菜单,这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:a_zhon="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways|snap"/> </android.support.design.widget.AppBarLayout> <android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <com.azhon.suspensionfab.SuspensionFab
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="bottom|right"
a_zhon:fab_orientation="top"
a_zhon:fab_spacing="4dp"/> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>我试过监听rootview的单击事件,结果无效 View rootView = ((ViewGroup)this.findViewById(android.R.id.content)).getChildAt(0);
rootView.setOnClickListener(this);
@Override
public void onClick(View v){
//close fab
}如何实现点击FloatingActionButton以外的view关闭fab的菜单?
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:a_zhon="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways|snap"/> </android.support.design.widget.AppBarLayout> <android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <com.azhon.suspensionfab.SuspensionFab
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="bottom|right"
a_zhon:fab_orientation="top"
a_zhon:fab_spacing="4dp"/> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>我试过监听rootview的单击事件,结果无效 View rootView = ((ViewGroup)this.findViewById(android.R.id.content)).getChildAt(0);
rootView.setOnClickListener(this);
@Override
public void onClick(View v){
//close fab
}如何实现点击FloatingActionButton以外的view关闭fab的菜单?
@Overridepublic void run() {// TODO Auto-generated method stubfor(int i = 0; ; i++){try {View view = rootView.findFocus();if (view != null) {int id = view.getId();Log.d(TAG, "id == " + id);}Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}
}}}).start();}}
我的代码:rootView = getWindow().getDecorView();
if(rootView != null){
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View v = rootView.findFocus();
Toast.makeText(activity,"id = "+v.getId(),Toast.LENGTH_SHORT).show();
}
});
}
好像并没有起作用,我在onClick里Log.d(),发现并没有打印日志,是我设置的监听器有问题?