从“设置页面”中删除FloatingActionButton

时间:2016-08-09 06:12:13

标签: android navigation-drawer application-settings floating-action-button android-navigation-drawer

所以我最近在我的应用程序中实现了一个导航抽屉。除了导航抽屉,我还实现了菜单项来访问设置等选项。但是,我想在所有导航抽屉页面上保留此浮动操作按钮,但我不希望在从菜单访问的设置页面上显示它。这是我当前的代码。

创建导航栏。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    //.setAction("Action", null).show();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

然后,这是app_bar_nav_drawer.xml

中浮动操作按钮本身的代码
 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.jamessingleton.chffrapi.NavDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_nav_drawer" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_menu_share"/>

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

以下是settings_layout.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:text="Use Cell Data to retrieve driving data:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="18sp"
        android:textColor="@color/cast_expanded_controller_background_color"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <RadioGroup
        android:layout_width="89dp"
        android:layout_height="69dp"
        android:weightSum="1"
        android:layout_below="@+id/textView">

        <RadioButton
            android:text="Enable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton7"
            android:layout_weight="1" />

        <RadioButton
            android:text="Disable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton6"
            android:layout_weight="1"
            android:checked="true" />
    </RadioGroup>


</RelativeLayout>

仅供参考我想仅保留菜单页面,而不是设置页面。如果我能添加更多有用的代码,请告诉我。

整个导航抽屉活动

    public class NavDrawerActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        //.setAction("Action", null).show();
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nav_drawer, 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();
        FragmentManager fragmentSettingsManager = getFragmentManager();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
            setTitle(R.string.action_settings);
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        FragmentManager fragmentManager = getFragmentManager();
        if (id == R.id.nav_first_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit();
            setTitle(R.string.speed_graph);
        } else if (id == R.id.nav_second_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit();
            setTitle(R.string.drive_player);
        } else if (id == R.id.nav_third_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit();
            setTitle(R.string.google_maps);
        } else if (id == R.id.nav_share) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!");
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

3 个答案:

答案 0 :(得分:0)

您只能在应隐藏它的那些活动中将“fab.setVisibility(View.GONE)”添加到OnCreate

希望这有帮助

答案 1 :(得分:0)

最佳做法是使用接口处理此类情况。 所以首先创建一个接口。

FabVisibilityController.java

public interface FabVisiblityController{
   toggleVisibility(boolean visibility);
}

然后在你的主要班级

NavigationDrawerActivity.java

public class NavigationDrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, FabVisibilityController{
    //write this code at the end of the class
    @override
    toggleVisibility(boolean visibility){
         if(visibility)
             fab.setVisibility(View.VISIBLE);
         else
             fab.setVisibility(View.GONE);
    }
}

然后在你的设置片段

public class SettingsFragment extends Fragment{
    Context context;
    FabVisibilityController controller;
    //then in onAttach method
    @override
    onAttach(Context context){
        this.context = context;
        if(context instanceof FabVisibilityController)
              controller = (FabVisibilityController) context;
    }

    //then in onResume Method
    @override
    onResume(){
        super.onResume();
        //this will hide FAB whenever settings page is called
        controller.toggleVisibility(false);
    }
}

在其他片段中执行相同操作,但只有差异,controller.toggleVisibility(true)应在onResume()中调用以使FAB可见。

答案 2 :(得分:0)

这是我最适合我的解决方案

#include <iostream>
using namespace std;

int main() {

    long long int i, n, factorial = 1;

    cout<<"Enter a positive integer: ";
    cin>>n;
    if (n<=0) {
        cout << "Please enter a non-negative number!!!\n";
    }

    else {

    for (i = 1; i <=n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;

    }
}
相关问题