FAB出现在不应该有FAB的活动中

时间:2016-06-21 21:21:37

标签: java android xml floating-action-button

所以我有一个Activity及其XML文件,它们都没有与fab有关(因为我删除了fab)。然而,当我构建并运行应用程序时,仍然会出现“幽灵工厂” - 我甚至无法点击它,它只是坐在角落里,我不知道如何删除它。

的活动:

package example.com.musicapptest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HostMainPage extends AppCompatActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_host_main_page);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}


@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);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Linking up values in fragment_main.xml to the MainActivity
        View rootView = inflater.inflate(R.layout.fragment_host_main_page, container, false);
        TextView splashTitleView = (TextView) rootView.findViewById(R.id.host_splash_title);
        TextView splashBodyView = (TextView) rootView.findViewById(R.id.host_splash_body);
        ProgressBar splashProgress = (ProgressBar) rootView.findViewById(R.id.host_splash_progres);
        Button hostButton = (Button) rootView.findViewById(R.id.host_start_button);
        hostButton.setVisibility(View.GONE);

        /*
            Setting up which splash page will be displayed as the user progresses throughout the welcome splash page
         */
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            splashTitleView.setText("Host Welcome");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(20);
        } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
            splashTitleView.setText("Create a Party ID");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(40);
        } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
            splashTitleView.setText("People Joint");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(60);
        } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 4) {
            splashTitleView.setText("Vote, Suggest, or View Leaderboards");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(80);
        }
        else if (getArguments().getInt(ARG_SECTION_NUMBER) == 5) {
            hostButton.setVisibility(View.VISIBLE);
            splashTitleView.setText("Start Hosting");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(100);
        }
        //Default frag if for some reason there is an IndexOutOfBoundsException()
        else {
            splashTitleView.setText("Get Started Fragment");
            splashBodyView.setText("Filler Text");
            splashProgress.setProgress(100);
        }

        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
            case 3:
                return "SECTION 4";
            case 4:
                return "SECTION 5";
        }
        return null;
    }
}

}

和XML:

<RelativeLayout 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: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="example.com.musicapptest.HostMainPage$PlaceholderFragment">

<TextView
    android:id="@+id/host_splash_title"
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:layout_marginTop="22dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<TextView
    android:layout_width="500dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/host_splash_body"
    android:layout_below="@id/host_splash_title"
    android:layout_centerHorizontal="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="20dp" />

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/host_splash_progres"
    android:layout_marginTop="48dp"
    android:layout_below="@+id/host_splash_body"
    android:layout_alignStart="@+id/host_splash_title" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/host_start_button"
    android:id="@+id/host_start_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="68dp" />

</RelativeLayout>

以下是我参考的图片:

Ghost FAB

谢谢!

1 个答案:

答案 0 :(得分:0)

您正在查看错误的布局XML文件。您正在布局中找到工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

但您粘贴的布局中没有工具栏。您必须有另一个名为activity_host_main_page.xml的布局文件,其中包含FAB。