gpt4 book ai didi

android - 用图标替换工具栏标题

转载 作者:行者123 更新时间:2023-11-29 15:03:10 25 4
gpt4 key购买 nike

大家好,我有一个包含三个 fragment 的 Activity 。当应用程序启动时,它会直接显示我的 Activity 和 Activity 名称。单击抽屉导航内的 fragment 时,工具栏标题会更改。现在我只想替换工具栏标题带有图标的 Activity 。为此,我使用了 setlogo。但是,我希望在应用程序首次启动时使图标可见。我的意思是,当我选择 fragment 时,我应该能够用 fragment 名称替换图标。

这是我的 Activity 代码,请帮忙

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>

最佳答案

那么,您需要自定义您的工具栏:如下所示:

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

然后你只需要根据你的要求处理imagetextvisibility

编辑

在您的 Activity 布局中包含工具栏布局:

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

关于android - 用图标替换工具栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41421638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com