如何使Focus成为FloatingActionButton即使导航抽屉在Android中打开

时间:2016-06-02 12:20:29

标签: android android-layout android-studio floating-action-button

我想关注FloatingActionButton即使我的导航抽屉是开放的。 我知道当导航抽屉打开时,屏幕的其余部分为unfocusable,使用setScrimColor这可以减少对导航抽屉的关注。但是我想让FloatingActionButton成为焦点。

图书馆位于ActionButton

的下方

https://github.com/shell-software/fab

请参阅以下屏幕截图,了解我的问题。

普通屏幕:

enter image description here

点击FloatingButton:

enter image description here

我想要的是这样的Focusable:

enter image description here

activity_main.xml中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <com.example.softeng.animationf.fabdirectory.ActionButton
            android:id="@+id/fab_activity_action_button"
            style="@style/fab_action_button_style"
            fab:type="MINI"/>


    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="end">

        <RelativeLayout
            android:id="@+id/right_navigation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#4D4D4D">

        </RelativeLayout>

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

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    DrawerLayout drawerLayout;
    NavigationView navigation_view;

    int count = 1;

    private boolean isOutSideClicked = false;


    RelativeLayout relativeLayout;
    private ActionButton actionButton;

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

        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
       /* drawerLayout.setScrimColor(Color.parseColor("#00000000"));*/
        navigation_view = (NavigationView)findViewById(R.id.navigation_view);
        relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);

        actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);

        actionButton.setImageResource(R.drawable.fab_plus_icon);
        actionButton.setRippleEffectEnabled(true);
        actionButton.setShadowRadius(0);
        actionButton.setShadowXOffset(0);
        actionButton.setShadowYOffset(0);
        actionButton.setButtonColor(Color.parseColor("#0072BA"));
        actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
        actionButton.setShadowRadius(10);

        actionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(count == 1) {
                    actionButton.moveLeft(200);
                    actionButton.setImageResource(R.drawable.close);
                    drawerLayout.openDrawer(Gravity.RIGHT);
                   count = count - 1;
                }else if(count == 0){

                    closeFab();


                }
                else {

                }
            }
        });

    }

    private void closeFab(){
        actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
        actionButton.setImageResource(R.drawable.fab_plus_icon);
        count = count + 1;
        drawerLayout.closeDrawer(Gravity.RIGHT);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (drawerLayout.isDrawerOpen(navigation_view)) {

                View content = findViewById(R.id.drawer_layout);
                int[] contentLocation = new int[2];
                content.getLocationOnScreen(contentLocation);
                Rect rect = new Rect(contentLocation[0],
                        contentLocation[1],
                        contentLocation[0] + content.getWidth(),
                        contentLocation[1] + content.getHeight());



                if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
                    isOutSideClicked = true;
                } else {
                    isOutSideClicked = false;
                    this.closeFab();
                }

            }
        }

        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

圆角图像表示我想要聚焦的FloatingActionButton。任何帮助都要得到赞赏。

2 个答案:

答案 0 :(得分:3)

找到问题的解决方案。我在您的布局以及Java代码中做了一些更改。如果有任何疑问,请参考并告诉我。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
NavigationView navigation_view;
ImageView view;
int count = 1;

private boolean isOutSideClicked = false;


RelativeLayout relativeLayout;
private ActionButton actionButton;

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

    view = (ImageView)findViewById(R.id.view);

    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }


    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawerLayout.setScrimColor(Color.parseColor("#00000000"));
    navigation_view = (NavigationView) findViewById(R.id.navigation_view);
    relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);

    actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);

    actionButton.setImageResource(R.drawable.fab_plus_icon);
    actionButton.setRippleEffectEnabled(true);
    actionButton.setShadowRadius(0);
    actionButton.setShadowXOffset(0);
    actionButton.setShadowYOffset(0);
    actionButton.setButtonColor(Color.parseColor("#0072BA"));
    actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
    actionButton.setShadowRadius(10);

    actionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(count == 1) {
                actionButton.moveLeft(200);
                actionButton.setImageResource(R.drawable.fab_plus_icon);
                drawerLayout.openDrawer(Gravity.RIGHT);
                view.setVisibility(View.VISIBLE);
                actionButton.bringToFront();
               count = count - 1;
            }else if(count == 0){

                closeFab();
            }
            else {

            }
        }
    });

}

private void closeFab(){
    view.setVisibility(View.GONE);
    actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
    actionButton.setImageResource(R.drawable.fab_plus_icon);
    count = count + 1;
    drawerLayout.closeDrawer(Gravity.RIGHT);
}
// dispatchTouchEvent is as it was not putting code here..!!
}

<强> activity_main.xml中

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80000000"
        android:visibility="gone" />

    <com.example.softeng.animationf.fabdirectory.ActionButton
        android:id="@+id/fab_activity_action_button"
        style="@style/fab_action_button_style"
        android:layout_gravity="bottom|right"
        fab:type="MINI"/>


</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="end">

    <RelativeLayout
        android:id="@+id/right_navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4D4D4D">

    </RelativeLayout>

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

</android.support.v4.widget.DrawerLayout>

请使用相同的代码。 我建议您进行复制粘贴,因为这样可以正常工作 .. !!

答案 1 :(得分:1)

使用Frame布局作为父ViewGroup,因为它使用z索引,所以放置

  

最后的FAB浮动操作按钮

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
<android.support.v4.widget.DrawerLayout

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_search"
        android:elevation="50dp"
        android:layout_gravity="left|bottom" />
</FrameLayout>

Floating button is clickable