2016-08-21 114 views
1

我使用導航抽屜,我希望我的應用程序的徽標出現在我的導航抽屜工具欄的中心。我在網上嘗試了很多解決方案,但我的徽標出現在預覽中,但是當我運行該應用程序時,它不在我的工具欄中。 這是我的應用程序欄佈局代碼:ImageView不會出現在導航抽屜工具欄

<?xml version="1.0" encoding="utf-8"?> 

<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:minHeight="?attr/actionBarSize" 
     app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:id="@+id/logoActionBar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:src="@drawable/actionbarlogo" /> 
     </RelativeLayout> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

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

這是我的導航抽屜的主要活動:

public class BaseActivity extends AppCompatActivity { 


    private DrawerLayout mDrawer; 
    private Toolbar toolbar; 
    private NavigationView nvDrawer; 
    private ActionBarDrawerToggle drawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home_customer); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     // toolbar.setLogo(R.drawable.actionbarlogo); 
// 
     setSupportActionBar(toolbar); 

     Constants cnst = new Constants(); 
     cnst.getNearbyusers(33.522212, 73.091904); 

     // getSupportActionBar().setIcon(R.drawable.logo_final); 
     mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     nvDrawer = (NavigationView) findViewById(R.id.nav_view); 
     setupDrawerContent(nvDrawer); 
     drawerToggle = setupDrawerToggle(); 
     mDrawer.addDrawerListener(drawerToggle); 
     // getSupportActionBar().setDisplayHomeAsUpEnabled(false); 

     View hView = nvDrawer.getHeaderView(0); 
     TextView nav_username = (TextView) hView.findViewById(R.id.fullNameUser); 
     TextView nav_emailuser = (TextView) hView.findViewById(R.id.emailUser); 
     ImageView nav_profilepic = (ImageView) hView.findViewById(R.id.ProfilePhoto); 

     Fragment home = new HomeFragment(); 
     FragmentManager FM = getSupportFragmentManager(); 
     FM 
       .beginTransaction() 
       .replace(R.id.container_layout, home) 
       .commit(); 

     SharedPreferences prefs = getApplicationContext().getSharedPreferences("HouseKeepingtg", Context.MODE_PRIVATE); 

     String username = prefs.getString("fName", null); 
     String email = prefs.getString("Email", null); 
     String profilepic = prefs.getString("Profilepic", null); 
     nav_username.setText(username); 
     nav_emailuser.setText(email); 
     if (profilepic != null) 
      nav_profilepic.setImageBitmap(decodeBase64(profilepic)); 

     Log.v("UserData", username + email); 


    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggles 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 

    private ActionBarDrawerToggle setupDrawerToggle() { 
     return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    } 

    private void setupDrawerContent(NavigationView navigationView) { 
     navigationView.setNavigationItemSelectedListener(
       new NavigationView.OnNavigationItemSelectedListener() { 
        @Override 
        public boolean onNavigationItemSelected(MenuItem menuItem) { 
         selectDrawerItem(menuItem); 
         return true; 
        } 
       }); 
    } 

    public void selectDrawerItem(MenuItem menuItem) { 
     Handler handler = new Handler(); 
     // Create a new fragment and specify the fragment to show based on nav item clicked 
     Fragment fragment = null; 
     Class fragmentClass; 
     switch (menuItem.getItemId()) { 
      case R.id.nav_display_worker_list: 
       fragmentClass = DisplayWorkersActivity.class; 
       break; 
      case R.id.nav_map: 
       fragmentClass = MapsActivity.class; 
       break; 
      case R.id.nav_home: 
       fragmentClass = HomeFragment.class; 
      case R.id.nav_settings: 
       fragmentClass = SettingsFragment.class; 
       break; 
//   case R.id.nav_update_profile: 
//     Intent intent = new Intent(BaseActivity.this, SignupRetailer.class); 
//     startActivity(intent); 
// 
//    break; 
      default: 
       fragmentClass = HomeFragment.class; 
     } 

     try { 
      fragment = (Fragment) fragmentClass.newInstance(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Insert the fragment by replacing any existing fragment 
     final FragmentManager fragmentManager = getSupportFragmentManager(); 
     final Fragment finalFragment = fragment; 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       fragmentManager.beginTransaction().replace(R.id.container_layout, finalFragment).commit(); 

      } 

     }, 250); 


     // Highlight the selected item has been done by NavigationView 
     menuItem.setChecked(true); 
     // Set action bar title 
     setTitle(menuItem.getTitle()); 
     // Close the navigation drawer 
     mDrawer.closeDrawers(); 


    } 


    @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.home_customer, 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); 
// } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    public static Bitmap decodeBase64(String input) { 
     byte[] decodedBytes = Base64.decode(input, 0); 
     return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); 
    } 


    public void clearStack() { 
/* 
* Here we are clearing back stack fragment entries 
*/ 
     int backStackEntry = getSupportFragmentManager().getBackStackEntryCount(); 

     if (backStackEntry > 0) { 
      for (int i = 0; i < backStackEntry; i++) { 
       getSupportFragmentManager().popBackStackImmediate(); 
      } 
     } 

/* 
* Here we are removing all the fragment that are shown here 
*/ 
     if (getSupportFragmentManager().getFragments() != null && getSupportFragmentManager().getFragments().size() > 0) { 
      for (int i = 0; i < getSupportFragmentManager().getFragments().size(); i++) { 
       Fragment mFragment = getSupportFragmentManager().getFragments().get(i); 
       if (mFragment != null) { 
        getSupportFragmentManager().beginTransaction().remove(mFragment).commit(); 
       } 
      } 
     } 
    } 


} 
+0

是'String profilepic = prefs.getString(「Profilepic」,null)的值;'在運行時有效嗎? – Chisko

+0

@Chisko是的,它是全部有效的,唯一的問題是imageView出現在工具欄 – Tayyaba

回答

0

雖然工具欄是一個ViewGroup中,您不需要像XML中的佈局一樣添加子視圖。你要麼

  • 添加特定的XML屬性實際的工具欄,像android.support.v7.appcompat:logo和其他

  • 使用代碼來添加視圖它,就像setLogo()和其他工具欄的功能。

您可以在文檔中閱讀全部內容。

+0

但setLogo()沒有集中的標誌,我希望它在酒吧中間 – Tayyaba

+0

你可以嘗試ViewGroup的'addView'方法,但它可能會弄亂整個工具欄。你也可以使用工具欄的ContentInsets,有很多功能。如果您需要,您可以跳過工具欄,並創建一個模仿您需要的工具欄功能的視圖。 – lionscribe

+0

是的,我想到了,但它會有導航抽屜功能? – Tayyaba