简体   繁体   中英

Action Bar Disappears on Fragment

I have 4 tabs on a tab layout. The first 3 work perfectly fine but the 4th one does hides the action bar. Presumably this is because there is too much content on the screen and its trying to fit all of the child elements on the screen. I would like for it to still show the action bar. Is there any way to do this? Below is some of the code:

Main Activity:

public class MainActivity extends AppCompatActivity {

        private SectionsPagerAdapter mSectionsPagerAdapter;


    private ViewPager mViewPager;


    private static final String SERVER_ADDRESS = "example.com/";
    private ImageView profilePic;

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

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        tabLayout.getTabAt(0).setIcon(R.drawable.ic_people_black_24dp);
        tabLayout.getTabAt(1).setIcon(R.drawable.ic_grade_black_24dp);
        tabLayout.getTabAt(2).setIcon(R.drawable.ic_mail_black_24dp);
        tabLayout.getTabAt(3).setIcon(R.drawable.ic_account_box_black_24dp);

        setTitle("Title");

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String username = intent.getStringExtra("username");
        String license = intent.getStringExtra("license");

    }

    @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) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            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) {
            switch (position) {
                case 0:
                    Browse browse = new Browse();
                    return browse;
                case 1:
                    MyAgents myAgents = new MyAgents();
                    return myAgents;
                case 2:
                    Messages messages = new Messages();
                    return messages;
                case 3:
                    MyProfile myProfile = new MyProfile();
                    return myProfile;

            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }
    }

Fragment that hides the action bar:

public class MyProfile extends Fragment {

    private static final String SERVER_ADDRESS = "test";
    private ImageView profilePic;
    private EditText etUsername;
    private EditText etLicense;
    private TextView welcomeMessage;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_profile, container, false);
        String message = getActivity().getIntent().getExtras().getString("name") + " welcome!";
        etUsername = (EditText) view.findViewById(R.id.etUsername);
        etLicense = (EditText) view.findViewById(R.id.etLicense);
        welcomeMessage = (TextView) view.findViewById(R.id.tvWelcomeMsg);
        profilePic = (ImageView) view.findViewById(R.id.ivProfilePicture);

        etUsername.setText(getActivity().getIntent().getExtras().getString("username"));
        etLicense.setText(getActivity().getIntent().getExtras().getString("license"));
        welcomeMessage.setText(message);

        new MyProfile.DownloadImage(getActivity().getIntent().getExtras().getString("license")).execute();

        return view;
    }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize" android:theme="@style/AppTheme"></activity>

    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

Layout of fragment:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <ImageView
        android:id="@+id/ivProfilePicture"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/ic_person_black_120dp"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/tvWelcomeMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="32dp"
        android:text="TextView"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username" />

    <EditText
            android:id="@+id/etUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="License"
            android:layout_marginTop="45dp" />

        <EditText
            android:id="@+id/etLicense"
            android:layout_width="352dp"
            android:layout_height="44dp"
            android:ems="10"
            android:inputType="text"
            android:text="License" />

</LinearLayout>
</ScrollView>

Anyone know how to fix this? I thought maybe since it was in a scroll view it would scroll and not cut off the top of the screen (where the action bar is). I've been looking everywhere and can't seem to find the solution to this problem. Any help would be very much appreciated.

Thank you.

从工具栏删除app:layout_scrollFlags =“ scroll | enterAlways”时,它将工具栏设置为固定位置并消除了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM