用图标替换工具栏标题

时间:2017-01-02 05:12:57

标签: android toolbar

大家好,我有一个包含三个片段的活动。当应用程序启动时,它会直接显示我的活动名称。点击导航栏中的片段,工具栏标题会发生变化。现在我只想更换工具栏标题带图标的活动。为此,我使用了setlogo.However我希望在应用程序启动时第一次看到图标。我的意思是当我选择片段时,我应该能够用片段名称替换图标。

这是我的活动代码。请帮忙

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>

3 个答案:

答案 0 :(得分:3)

嗯,您需要自定义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>

之后,您只需根据您的要求处理visibility imagetext

修改

将活动布局中的工具栏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);

答案 1 :(得分: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();
}

答案 2 :(得分: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);

        }