行动栏消失在片段

时间:2017-07-07 00:46:18

标签: android android-fragments android-actionbar android-tablayout

我在标签布局上有4个标签。前3个工作完美,但第4个确实隐藏了动作栏。据推测,这是因为屏幕上的内容太多,并且试图将所有子元素都放在屏幕上。我想它仍然显示动作栏。有没有办法做到这一点?以下是一些代码:

主要活动:

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

隐藏操作栏的片段:

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

清单:

<?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>

片段布局:

<?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>

任何人都知道如何解决这个问题?我想也许因为它是在滚动视图中它会滚动而不是切断屏幕的顶部(动作栏所在的位置)。我一直在寻找无处不在,似乎无法找到解决这个问题的方法。非常感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

删除app时:layout_scrollFlags =&#34;滚动| enterAlways&#34;从工具栏中将工具栏设置为固定位置并删除此问题。

相关问题