Java 工具栏不显示应用程序名称和后退按钮
因此,由于我之前在这里发布的工具栏阴影的问题,我想到了如何删除它的想法。现在至少没有阴影,但没有活动名称或后退按钮。见下图 下面是content.xmlJava 工具栏不显示应用程序名称和后退按钮,java,android,xml,Java,Android,Xml,因此,由于我之前在这里发布的工具栏阴影的问题,我想到了如何删除它的想法。现在至少没有阴影,但没有活动名称或后退按钮。见下图 下面是content.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.Vjezbe"
tools:showIn="@layout/activity_vjezbe">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Vježbe"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
>
</include>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
app:tabMode="scrollable"
app:tabGravity="fill"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
android:background="#ffffff">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
正如您所看到的,我在java文件中尝试了一些if语句,但没有成功
我遗漏了什么?删除
if(toolbar==null)
成功了
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
除去
if(toolbar==null)
成功了
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
问题
如果(工具栏==null)
解决方案
使用!空
检查而不是空
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
编辑
// toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
//Your code
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
工作正常
通过添加android:elevation=“0dp”问题
如果(工具栏==null)
解决方案
使用!空
检查而不是空
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
编辑
// toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
//Your code
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
工作正常
通过添加
android:elevation=“0dp”
只需在创建以下事件时添加此代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);//set view
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);// initial your tool bar
setSupportActionBar(toolbar);//set tool bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//allow back button
getSupportActionBar().setDisplayShowHomeEnabled(true);//Show back arrow
getSupportActionBar().setLogo(R.mipmap.yourlogo);//set logo
getSupportActionBar().setDisplayUseLogoEnabled(true);//enable log
}
只需在创建事件时向您添加以下代码,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);//set view
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);// initial your tool bar
setSupportActionBar(toolbar);//set tool bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//allow back button
getSupportActionBar().setDisplayShowHomeEnabled(true);//Show back arrow
getSupportActionBar().setLogo(R.mipmap.yourlogo);//set logo
getSupportActionBar().setDisplayUseLogoEnabled(true);//enable log
}
这样做了,但是仍然没有标题和后退按钮显示那个,但是仍然没有标题和后退按钮显示那个,但是仍然没有标题和后退按钮
toolbar=(toolbar)findViewById(R.id.toolbar);工具栏。设置标题(“标题”);设置支持操作栏(工具栏);工具栏.setNavigationIcon(R.drawable.your_back_图标)代码>@DaxHR供测试用例使用以上感谢您的时间。我解决了这个问题,在activity.xml中删除了工具栏,并将提升设置为0,而不是像我以前尝试的那样在content.xml中设置为0是的,我一直试图将该行放在content.xml中,但显然应该放在activity.xml中,但仍然没有标题和后退按钮toolbar=(toolbar)findViewById(R.id.toolbar);工具栏。设置标题(“标题”);设置支持操作栏(工具栏);工具栏.setNavigationIcon(R.drawable.your_back_图标)代码>@DaxHR供测试用例使用以上感谢您的时间。我通过移除工具栏并在activity.xml中将提升设置为0,而不是像我以前尝试的那样在content.xml中设置为0,解决了这个问题是的,我一直尝试将该行放在content.xml中,但显然应该放在activity.xml中