我的应用程序名称Zenica未出现在我的工具栏中,它出现在工具栏下方。作为参考,我附上了它的图像。此外,我希望保持工具栏的颜色与窗口的其他部分不同,但在toolbar.xml中设置背景时,整个窗口的颜色会发生变化,而不是仅更改工具栏颜色。
HomeActivity.java
package com.zenica.app.zenica;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Slide;
import android.transition.Transition;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
/**
* Created by Neha on 03-02-2016.
*/
public class HomeActivity extends AppCompatActivity {
private Toolbar mtoolar;
DrawerLayout dLayout;
private NavigationView navView;
private ActionBarDrawerToggle mDrawerToggle;
@TargetApi(21)
@Override
public void onCreate(Bundle savedInstanceState) {
int currentapiVersion = Build.VERSION.SDK_INT;
if (currentapiVersion == Build.VERSION_CODES.LOLLIPOP) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
Transition ts = new Slide();
ts.setDuration(3000);
getWindow().setEnterTransition(ts);
getWindow().setExitTransition(ts);
setContentView(R.layout.navigation_drawer);
} else {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
}
setToolbar();
setNavigationDrawer();
}
private void setToolbar()
{
mtoolar=(Toolbar) findViewById(R.id.tool_bar);
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
setSupportActionBar(mtoolar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(5);
mDrawerToggle = new ActionBarDrawerToggle(this, dLayout,mtoolar, R.string.drawer_open, R.string.drawer_close)
{
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
// Set the drawer toggle as the DrawerListener
dLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
private void setNavigationDrawer() {
dLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
navView=(NavigationView) findViewById(R.id.navigation);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int itemId = menuItem.getItemId();
if (itemId == R.id.home_item) {
}
else if (itemId == R.id.about_app) {
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tool_bar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme"
android:elevation="8dp">
</android.support.v7.widget.Toolbar>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zenica.app.zenica" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow">
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:theme="@style/AppTheme">
</activity>
</application>
</manifest>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorPrimaryLight">#C5CAE9</color>
<color name="textColorPrimary">#212121</color>
<color name="textColorSecondary">#727272</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="colorAccent">#FF5252</color>
<color name="icons">#FFFFFF</color>
<color name="listDivider">#68FF66</color>
</resources>
navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:attr/listDivider"
tools:context=".HomeActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:itemTextColor="#333"
app:itemIconTint="#ddd"
app:menu="@menu/navigation_items"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"/>
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>