2017-07-31 136 views
0

我創建了一個導航抽屜,導航菜單中有一個選項是配置文件,每當我嘗試點擊配置文件中的一個配置文件片段活動打開,因爲我做了該活動的意圖,但該視圖的xml活動根本不可見。只有空白屏幕出現,沒有背景。誰能幫我嗎。導航片段在android中工作

activity_profile_fragment.xml

     <?xml version="1.0" encoding="utf-8"?> 
      <android.support.constraint.ConstraintLayout 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" 
       tools:context="com.example.shubham.JeevanSathi.Activity.ProfileFragment" 
       android:background="#FFF"> 



       <ScrollView 
        android:layout_width="368dp" 
        android:layout_height="495dp" 
        tools:layout_editor_absoluteY="8dp" 
        tools:layout_editor_absoluteX="8dp" 
        > 


       <RelativeLayout 
        android:layout_width="368dp" 
        android:layout_height="495dp" 

        tools:layout_editor_absoluteY="8dp" 
        tools:layout_editor_absoluteX="8dp"> 



        <TextView 
         android:id="@+id/tv2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="0dp" 
         android:layout_marginLeft="110dp" 
         android:layout_marginStart="110dp" 
         android:layout_marginTop="20dp" 


         android:text="My Profile" 
         android:textColor="@android:color/black" 
         android:textSize="33sp" 
         android:textStyle="bold" 
         android:typeface="sans" /> 

        <TextView 
         android:id="@+id/tv3" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="0dp" 
         android:layout_marginLeft="112dp" 
         android:layout_marginStart="112dp" 
         android:layout_marginTop="20dp" 


         android:text="My Profile" 
         android:textColor="@android:color/holo_green_dark" 
         android:textSize="32sp" 
         android:textStyle="bold" 
         android:typeface="sans" /> 

        <ImageView 
         android:layout_width="80dp" 
         android:layout_height="90dp" 
         android:id="@+id/profile_photo" 
         android:layout_marginTop="96dp" 
         android:layout_alignParentTop="true" 
         android:layout_centerHorizontal="true" 
         android:outlineProvider="bounds"/> 

        <LinearLayout 
         android:id="@+id/ll1" 
         android:layout_width="100dp" 
         android:layout_height="match_parent" 
         android:orientation="vertical" 
         android:layout_below="@+id/profile_photo" 
         android:layout_marginTop="15dp" 
         android:layout_marginStart="30dp" 
         android:layout_marginLeft="30dp" 
         > 

         <TextView 
          android:id="@+id/name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="10dp" 

          android:text="Name:" 
          android:textColor="@android:color/background_dark" /> 

         <TextView 
          android:id="@+id/age" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Age:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/religion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Religion:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/address" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Address:" 

         android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/zodiacsign" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Zodiac Sign:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/dob" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Date of Birth:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/bloddgroup" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Blood Group:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/fathername" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Father's Name:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/mothername" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="15dp" 
          android:text="Mother's Name:" 
          android:textColor="@android:color/background_dark" 
          /> 

         <TextView 
          android:id="@+id/sibling" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="No. of Siblings:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/height" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Height:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/weight" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Weight:" 
          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/hobbies" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Hobbies:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 


         <TextView 
          android:id="@+id/complexion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="complexion:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 

         <TextView 
          android:id="@+id/langknw" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Language Known:" 

          android:layout_marginTop="15dp" 
          android:textColor="@android:color/background_dark"/> 
        </LinearLayout> 


        <LinearLayout 
         android:layout_width="100dp" 
         android:layout_height="match_parent" 
         android:orientation="vertical" 
         android:layout_below="@+id/profile_photo" 
         android:layout_toRightOf="@+id/ll1" 
         android:layout_marginTop="15dp" 
         android:layout_marginStart="50dp" 
         android:layout_marginLeft="30dp"> 

         <TextView 
          android:id="@+id/disname" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="XYZ" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="10dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disage" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="23" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disreligion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="hindu" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disaddress" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="pune" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/diszodiacsign" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Aquarius" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disdob" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="10/02/1993" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disbloddgroup" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="B+" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disfathername" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="abc" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/dismothername" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="15dp" 
          android:text="pqr" 
          android:textColor="@android:color/background_dark" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/dissibling" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="2 sisters" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disheight" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="5'11" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/disweight" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="63" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/dishobbies" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Drawing" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 


         <TextView 
          android:id="@+id/discomplexion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Wheaty" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 

         <TextView 
          android:id="@+id/dislangknw" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Hindi, English, Gujrathi, Marathi" 
          android:textColor="@android:color/background_dark" 
          android:layout_marginTop="15dp" 
          android:inputType="text"/> 
        </LinearLayout> 


       </RelativeLayout> 
       </ScrollView> 

      </android.support.constraint.ConstraintLayout> 

MainActivity.java

    public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener { 

         private NavigationView navigationView; 
         private DrawerLayout drawer; 
         private View navHeader; 
         private ImageView imgNavHeaderBg, imgProfile; 
         private TextView txtName, txtWebsite; 
         private Toolbar toolbar; 
         private FloatingActionButton fab; 
         ActionBar actionBar; 
         //This is our tablayout 
         private TabLayout tabLayout; 

         //This is our viewPager 
         private ViewPager viewPager; 

         // urls to load navigation header background image 
         // and profile image 
         private static final String urlNavHeaderBg = "http://api.androidhive.info/images/nav-menu-header-bg.jpg"; 
         private static final String urlProfileImg = "https://lh3.googleusercontent.com/eCtE_G34M9ygdkmOpYvCag1vBARCmZwnVS6rS5t4JLzJ6QgQSBquM0nuTsCpLhYbKljoyS-txg"; 

         // index to identify current nav menu item 
         public static int navItemIndex = 0; 

         // tags used to attach the fragments 
         private static final String TAG_HOME = "home"; 
         private static final String TAG_PROFILE = "profile"; 
         private static final String TAG_MOVIES = "movies"; 
         private static final String TAG_NOTIFICATIONS = "notifications"; 
         private static final String TAG_SETTINGS = "settings"; 
         public static String CURRENT_TAG = TAG_HOME; 

         // toolbar titles respected to selected nav menu item 
         private String[] activityTitles; 

         // flag to load home fragment when user presses back key 
         private boolean shouldLoadHomeFragOnBackPress = true; 
         private Handler mHandler; 

         ArrayList<ToDoListPojo> arr; 
         ListView lst,lst1; 
         EditText edtTitle,edtDesc, tname,tdesc; 
         ToDoListAdapter tAdapter; 
         ImageView iv,iv1; 

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

          Intent intent= getIntent(); 

          //declarations for list view 
          arr = new ArrayList<>(); 
          lst = (ListView) findViewById(R.id.bride_list_view); 
          lst1 = (ListView) findViewById(R.id.groom_list_view); 
          edtTitle= (EditText) findViewById(R.id.edtTitle); 
          edtDesc = (EditText) findViewById(R.id.edtDescription); 
          tname=(EditText) findViewById(R.id.name); 
          tdesc=(EditText) findViewById(R.id.desc); 
          iv= (ImageView) findViewById(R.id.icon); 
          iv1= (ImageView) findViewById(R.id.icon1); 
          tAdapter = new ToDoListAdapter(this,arr); 
          lst.setAdapter(tAdapter); 


          actionBar = getSupportActionBar(); 
          actionBar.show(); 
          toolbar = (Toolbar) findViewById(R.id.toolbar); 
          // setSupportActionBar(toolbar); 

          mHandler = new Handler(); 

          drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

          navigationView = (NavigationView) findViewById(R.id.nav_view); 

          View hView = navigationView.getHeaderView(0); 

          // fab = (FloatingActionButton) findViewById(R.id.fab); 

          // Navigation view header 
          navHeader = navigationView.getHeaderView(0); 
          txtName = (TextView) navHeader.findViewById(R.id.name); 
          txtWebsite = (TextView) navHeader.findViewById(R.id.website); 
          imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg); 
          imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile); 

          // load toolbar titles from string resources 
          activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles); 

          /* 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(); 
           } 
          });*/ 

          // load nav menu header data 
          loadNavHeader(); 

          // initializing navigation menu 
          setUpNavigationView(); 

          if (savedInstanceState == null) { 
           navItemIndex = 0; 
           CURRENT_TAG = TAG_HOME; 
           loadHomeFragment(); 
          } 

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

          //Initializing the tablayout 
          tabLayout = (TabLayout) findViewById(R.id.tabLayout); 

          //Adding the tabs using addTab() method 
          tabLayout.addTab(tabLayout.newTab().setText("Home")); 
          tabLayout.addTab(tabLayout.newTab().setText("Search Bride")); 
          tabLayout.addTab(tabLayout.newTab().setText("Search Groom")); 
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

          //Initializing viewPager 
          viewPager = (ViewPager) findViewById(R.id.pager); 

          //Creating our pager adapter 
          Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount()); 

          //Adding adapter to pager 
          viewPager.setAdapter(adapter); 

          tabLayout.setOnTabSelectedListener(this); 
          viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 


           @Override 
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
            tabLayout.setScrollPosition(position, positionOffset, true); 
           } 

           @Override 
           public void onPageSelected(int position) { 

           } 

           @Override 
           public void onPageScrollStateChanged(int state) { 

           } 

           //Adding onTabSelectedListener to swipe views 

           //tabLayout.setOnClickListener(); 


          }); 
         } 


         public void addItem(View v) 
         { 
          String title = edtTitle.getText().toString(); 
          String des = edtDesc.getText().toString(); 
          String Tname = tname.getText().toString(); 
          String Tdesc = tdesc.getText().toString(); 

          ToDoListPojo t = new ToDoListPojo(); 
          t.setTitle(title); 
          t.setDescription(des); 
          t.setTname(Tname); 
          t.setTdesc(Tdesc); 


          arr.add(t); 
          tAdapter.notifyDataSetChanged(); 
         } 

         public void det(View view) { 

          Intent i2 = new Intent(MainActivity.this,bio_data.class); 

          startActivity(i2); 
         } 


         /*** 
         * Load navigation menu header information 
         * like background image, profile image 
         * name, website, notifications action view (dot) 
         */ 
         private void loadNavHeader() { 
          // name, website 
          txtName.setText("shubham"); 
          txtWebsite.setText("[email protected]"); 

          // loading header background image 
          Glide.with(this).load(urlNavHeaderBg) 
            .crossFade() 
            .diskCacheStrategy(DiskCacheStrategy.ALL) 
            .into(imgNavHeaderBg); 

          // Loading profile image 
          Glide.with(this).load(urlProfileImg) 
            .crossFade() 
            .thumbnail(0.5f) 
            .bitmapTransform(new CircleTransform(this)) 
            .diskCacheStrategy(DiskCacheStrategy.ALL) 
            .into(imgProfile); 

          // showing dot next to notifications label 
          navigationView.getMenu().getItem(3).setActionView(R.layout.menu_dot); 
         } 

         /*** 
         * Returns respected fragment that user 
         * selected from navigation menu 
         */ 
         private void loadHomeFragment() { 
          // selecting appropriate nav menu item 
          selectNavMenu(); 

          // set toolbar title 
          setToolbarTitle(); 

          // if user select the current navigation menu again, don't do anything 
          // just close the navigation drawer 
          if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) { 
           drawer.closeDrawers(); 

           // show or hide the fab button 
           //toggleFab(); 
           return; 
          } 

          // Sometimes, when fragment has huge data, screen seems hanging 
          // when switching between navigation menus 
          // So using runnable, the fragment is loaded with cross fade effect 
          // This effect can be seen in GMail app 
          Runnable mPendingRunnable = new Runnable() { 
           @Override 
           public void run() { 
            // update the main content by replacing fragments 
            HomeFragment fragment = getHomeFragment(); 
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, 
              android.R.anim.fade_out); 
            // FragmentTransaction replace = fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG); 
            fragmentTransaction.commitAllowingStateLoss(); 
           } 
          }; 

          // If mPendingRunnable is not null, then add to the message queue 
          if (mPendingRunnable != null) { 
           mHandler.post(mPendingRunnable); 
          } 

          // show or hide the fab button 
          // toggleFab(); 

          //Closing drawer on item click 
          drawer.closeDrawers(); 

          // refresh toolbar menu 
          invalidateOptionsMenu(); 
         } 

         private HomeFragment getHomeFragment() { 
          switch (navItemIndex) { 
           case 0: 
            // home 
            HomeFragment homeFragment = new HomeFragment(); 
            return homeFragment; 
           case 1: 
            // profile 

            ProfileFragment profileFragment = new ProfileFragment(); 
            return profileFragment; 
           case 2: 
            // movies fragment 
            MoviesFragment moviesFragment = new MoviesFragment(); 
            return moviesFragment; 
           case 3: 
            // notifications fragment 
            NotificationFragment notificationsFragment = new NotificationFragment(); 
            return notificationsFragment; 

           case 4: 
            // settings fragment 
            SettingsFragment settingsFragment = new SettingsFragment(); 
            return settingsFragment; 
           default: 
            return new HomeFragment(); 
          } 
         } 

         private void setToolbarTitle() { 
          getSupportActionBar().setTitle(activityTitles[navItemIndex]); 
         } 

         private void selectNavMenu() { 
          navigationView.getMenu().getItem(navItemIndex).setChecked(true); 
         } 

         private void setUpNavigationView() { 
          //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

           // This method will trigger on item Click of navigation menu 
           @Override 
           public boolean onNavigationItemSelected(MenuItem menuItem) { 

            //Check to see which item was being clicked and perform appropriate action 
            switch (menuItem.getItemId()) { 
             //Replacing the main content with ContentFragment Which is our Inbox View; 
             case R.id.nav_home: 
              navItemIndex = 0; 
              CURRENT_TAG = TAG_HOME; 
              break; 
             case R.id.nav_profile: 

              Intent i3= new Intent(MainActivity.this,ProfileFragment.class); 
              startActivity(i3); 
              navItemIndex = 1; 
              CURRENT_TAG = TAG_PROFILE; 
              break; 
             /* case R.id.nav_movies: 
              navItemIndex = 2; 
              CURRENT_TAG = TAG_MOVIES; 
              break;*/ 
             case R.id.nav_notifications: 
              navItemIndex = 3; 
              CURRENT_TAG = TAG_NOTIFICATIONS; 
              break; 
             case R.id.nav_settings: 
              navItemIndex = 4; 
              CURRENT_TAG = TAG_SETTINGS; 
              break; 
             case R.id.nav_about_us: 
              // launch new intent instead of loading fragment 
              startActivity(new Intent(MainActivity.this, AboutUsActivity.class)); 
              drawer.closeDrawers(); 
              return true; 
             /*case R.id.nav_privacy_policy: 
              // launch new intent instead of loading fragment 
              startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class)); 
              drawer.closeDrawers(); 
              return true;*/ 
             default: 
              navItemIndex = 0; 
            } 

            //Checking if the item is in checked state or not, if not make it in checked state 
            if (menuItem.isChecked()) { 
             menuItem.setChecked(false); 
            } else { 
             menuItem.setChecked(true); 
            } 
            menuItem.setChecked(true); 

            loadHomeFragment(); 

            return true; 
           } 
          }); 


          ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer,toolbar, R.string.openDrawer, R.string.closeDrawer) { 

           @Override 
           public void onDrawerClosed(View drawerView) { 
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
            super.onDrawerClosed(drawerView); 
           } 

           @Override 
           public void onDrawerOpened(View drawerView) { 
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 
            super.onDrawerOpened(drawerView); 
           } 
          }; 

          //Setting the actionbarToggle to drawer layout 
          drawer.setDrawerListener(actionBarDrawerToggle); 

          //calling sync state is necessary or else your hamburger icon wont show up 
          actionBarDrawerToggle.syncState(); 
         } 


         @Override 
         public void onBackPressed() { 
          if (drawer.isDrawerOpen(GravityCompat.START)) { 
           drawer.closeDrawer(GravityCompat.START); 
           return; 
          }else 
          { 
           super.onBackPressed(); 
          } 

          // This code loads home fragment when back key is pressed 
          // when user is in other fragment than home 
          if (shouldLoadHomeFragOnBackPress) { 
           // checking if user is on other navigation menu 
           // rather than home 
           if (navItemIndex != 0) { 
            navItemIndex = 0; 
            CURRENT_TAG = TAG_HOME; 
            loadHomeFragment(); 
            return; 
           } 
          } 

          super.onBackPressed(); 
         } 

         @Override 
         public boolean onCreateOptionsMenu(Menu menu) { 
          // Inflate the menu; this adds items to the action bar if it is present. 

          // show menu only when home fragment is selected 
          if (navItemIndex == 0) { 
           getMenuInflater().inflate(R.menu.main, menu); 
          } 

          // when fragment is notifications, load the menu created for notifications 
          if (navItemIndex == 3) { 
           getMenuInflater().inflate(R.menu.notifications, 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_logout) { 
           Toast.makeText(getApplicationContext(), "Logout user!", Toast.LENGTH_LONG).show(); 
           return true; 
          } 

          // user is in notifications fragment 
          // and selected 'Mark all as Read' 
          if (id == R.id.action_mark_all_read) { 
           Toast.makeText(getApplicationContext(), "All notifications marked as read!", Toast.LENGTH_LONG).show(); 
          } 



          // user is in notifications fragment 
          // and selected 'Clear All' 
          if (id == R.id.action_clear_notifications) { 
           Toast.makeText(getApplicationContext(), "Clear all notifications!", Toast.LENGTH_LONG).show(); 
          }drawer.closeDrawer(Gravity.START); 

          return super.onOptionsItemSelected(item); 
         } 

         // show or hide the fab 
         /* private void toggleFab() { 
          if (navItemIndex == 0) 
           fab.show(); 
          else 
           fab.hide(); 
         } 
        */ 



         @Override 
         public void onTabSelected(TabLayout.Tab tab) { 

          viewPager.setCurrentItem(tab.getPosition()); 

         } 

         @Override 
         public void onTabUnselected(TabLayout.Tab tab) { 


         } 

         @Override 
         public void onTabReselected(TabLayout.Tab tab) { 

         } 


         public void onSectionAttached(Object argSectionNumber) { 
         } 
        } 
+0

發表您的片段的XML –

回答

0

在該片段的XML,在父母的佈局設置使用下面的代碼白色背景:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFF"> 

<TextView 
    android:id="@+id/txtData" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:layout_centerInParent="true" 
    android:text="No data received" /> 


</RelativeLayout> 
+0

謝謝你,但它沒有奏效。我有約束佈局作爲父佈局 –

+0

你可以發佈你的XML? –

+0

請參閱上面的xml –