2017-01-02 90 views
1

你好我所有的活動都有三個片段。當應用程序啓動它時,直接顯示我的活動與活動名稱。並點擊導航抽屜裏面的片段工具欄標題changes.Now我只想要替換工具欄標題只是帶有圖標的活動。爲此,我使用了setlogo.However但我希望只在應用程序啓動時第一次顯示圖標。我的意思是當我選擇片段時,我應該能夠用片段名稱替換圖標。用圖標替換工具欄標題

這是我activity.Please的代碼幫助

package com.example.user.educationhunt; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.example.user.educationhunt.fragment.NearMe; 
import com.example.user.educationhunt.fragment.Register; 
import com.example.user.educationhunt.fragment.Search; 
import com.example.user.educationhunt.fragment.Settings; 
import com.example.user.educationhunt.pojos.feedbackData; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

public class EduHunt 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_edu_hunt); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     toolbar.setLogo(R.mipmap.edutext); 

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

     nvDrawer = (NavigationView) findViewById(R.id.nvView); 
     setupDrawerContent(nvDrawer); 

     // Find our drawer view 
     mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawerToggle = setupDrawerToggle(); 

     // Tie DrawerLayout events to the ActionBarToggle 
     mDrawer.addDrawerListener(drawerToggle); 

     FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
     tx.replace(R.id.flContent, new Search()); 
     tx.commit(); 


    } 

    private ActionBarDrawerToggle setupDrawerToggle() { 
     return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.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) { 
     // 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.search: 
       fragmentClass = Search.class; 
       break; 
      case R.id.settings: 
       fragmentClass = Settings.class; 
       break; 
      case R.id.register: 
       fragmentClass = Register.class; 
       break; 
      case R.id.nearme: 
       fragmentClass = NearMe.class; 
       break; 
      default: 
       fragmentClass = Search.class; 
     } 

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

     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); 

     menuItem.setChecked(true); 
     setTitle(menuItem.getTitle()); 
     mDrawer.closeDrawers(); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     switch (item.getItemId()){ 
      case R.id.our_team: 
       final Dialog dialog=new Dialog(this); 
       dialog.setContentView(R.layout.activity_our_team); 
       dialog.show(); 
       return true; 
      case R.id.feedback: 
       startActivity(new Intent(EduHunt.this,SendFeedback.class)); 
       return true; 

     } 
     return super.onOptionsItemSelected(item); 
    } 


    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.education_hunt, menu); 
     return true; 
    } 
} 

這是我toolbar.xml

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true" 
    android:minHeight="?attr/actionBarSize" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:background="?attr/colorPrimaryDark"> 
</android.support.v7.widget.Toolbar> 

回答

5

那麼,你需要你的toolbar定製:如下:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.Toolbar 
     android:layout_height="56dp" 
     android:layout_width="match_parent" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:background="@color/colorPrimary" 
     app:theme="@style/ToolbarSearchView" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="56dp" 
      android:orientation="horizontal" 
      android:id="@+id/ll_navi" 

      android:background="@color/colorPrimary" 
      android:gravity="center_vertical" 

      > 


      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="46dp"     
       android:paddingLeft="5dp" 
       .... 
       /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="46dp"     
       android:paddingLeft="5dp" 
      ..../> 

    </LinearLayout> 


    </android.support.v7.widget.Toolbar> 

之後,你只需要處理的imagevisibilitytext根據您的要求。

編輯

包括在你活動的佈局工具欄layout

<LinearLayout 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:gravity="center_horizontal" 
    android:orientation="vertical" 

    android:background="@drawable/bg"> 
    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     /> 
     ........ 
      </LineraLayout> 

,然後你Activity訪問工具欄爲:

 setContentView(R.layout.activity_login); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     TextView txtTitle = (TextView) findViewById(R.id.toolbar_title); 
+0

如何訪問工具欄中的圖像或文字 –

+0

@EdwardMintus plz檢查我的更新答案。 –

+0

非常感謝你 –

1

試試這個。

toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
toolbar.setNavigationIcon(R.drawable.yourIcon); 
toolbar.setTitle(""); 

變化在這裏:

public void selectDrawerItem(MenuItem menuItem) { 
    // Create a new fragment and specify the fragment to show based on nav item clicked 
    Fragment fragment = null; 
    Class fragmentClass; 
    toolbar.setNavigationIcon(R.drawable.yourIcon); 

    switch (menuItem.getItemId()) { 
     case R.id.search: 
      fragmentClass = Search.class; 
     //Change it here 
      toolbar.setNavigationIcon(R.drawable.cutomIcon); 
      break; 

     case R.id.settings: 
    //Change it here 
     toolbar.setNavigationIcon(R.drawable.cutomIcon);    
     fragmentClass = Settings.class; 
      break; 

     case R.id.register: 
     //Change it here 
     toolbar.setNavigationIcon(R.drawable.cutomIcon);    
     fragmentClass = Register.class; 

      break; 

     case R.id.nearme: 
     //Change it here 
     toolbar.setNavigationIcon(R.drawable.cutomIcon); 
     fragmentClass = NearMe.class; 
      break; 

     default: 
     //Change it here 
     toolbar.setNavigationIcon(R.drawable.cutomIcon); 
     fragmentClass = Search.class;                     
    } 

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

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.flContent,  fragment).commit(); 

    menuItem.setChecked(true); 
    setTitle(menuItem.getTitle()); 
    mDrawer.closeDrawers(); 
} 
+0

Bro我不想使用任何圖標的fragment.What是customIcon的? –

+0

哦,好的。對不起,我誤解了這個問題。 –

+0

我以爲你想改變片段變化的圖標。這樣,您可以在選擇片段時更改圖標。 customIcon是你想要使用的圖標。 –

1

試試這個,

public void selectDrawerItem(MenuItem menuItem) { 
      // 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.search: 
        toolbar.setTitle("search"); 
        fragmentClass = Search.class; 
        break; 
       case R.id.settings: 
        toolbar.setTitle("settings"); 
        fragmentClass = Settings.class; 
        break; 
       case R.id.register: 
        fragmentClass = Register.class; 
        break; 
       case R.id.nearme: 
        fragmentClass = NearMe.class; 
        break; 
       default: 
        fragmentClass = Search.class; 
      } 

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

      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); 

      menuItem.setChecked(true); 
      setTitle(menuItem.getTitle()); 
      mDrawer.closeDrawers(); 
      toolbar.setLogo(null); 

     }