Android 工具栏未显示在其他活动中
Idk为什么但我的自定义工具栏没有显示在不同的活动中! 在我的主要活动中Android 工具栏未显示在其他活动中,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,Idk为什么但我的自定义工具栏没有显示在不同的活动中! 在我的主要活动中 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="#fff" app
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#fff"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
在我的清单中:
<activity
android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.dell.x.Main" />
</activity>
我的风格:
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
符合事实的
错误的
但是,除主工具栏外,我的工具栏不会显示在任何其他活动中!我想显示带有自定义标题的后退按钮。您在
窗口操作栏中设置了false
试试这个
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
符合事实的
符合事实的
及
希望这对您有所帮助。在主要活动的XML布局文件中,您将看到如下内容
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
...
other controls
...
</android.support.design.widget.CoordinatorLayout>
基本上,如果您不想使用与默认工具栏协调的布局,请使用工具栏\u布局
!
您可以创建以下内容:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@color/white"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_height="?attr/actionBarSize"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content">
<ImageView />
<TextView/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
在主布局中添加
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
我认为这很好用 试试这个@nimi0112看到了吗,但不知道如何帮助我。你需要将工具栏单独添加到所有活动中。我在我的其他活动中已经这样做了code getSupportActionBar().setTitle(“Kompania”)代码>在空对象引用上尝试此id android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)“”或者此id没有修复此id。此id改进了布局,但通过“其他活动”在其他活动中仍然没有显示任何内容。您是指片段吗?您是指将其添加到所有其他布局中吗?是的,或者主活动xml中的任何内容,这可能只是一个问题
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
...
other controls
...
</android.support.design.widget.CoordinatorLayout>
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@color/white"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_height="?attr/actionBarSize"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content">
<ImageView />
<TextView/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}