工具栏未显示应用名称和后退按钮

时间:2016-02-15 09:17:38

标签: java android xml

因为我之前在这里发布的工具栏阴影问题,我想出了如何删除它。现在至少没有阴影,但没有活动名称或后退按钮。见下图

Image

这是content.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    android:id="@+id/toolBar"

    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


</android.support.v7.widget.Toolbar>

这是toolbar.xml

package hr.app.liftme.liftmehr;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class Vjezbe extends AppCompatActivity {
    private Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vjezbe);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar == null){


        setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Vježbe");
            getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setElevation(0);
    }


        tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
//here are the fragments, not going to copy all of that

java文件

String menuName = (String) JOptionPane.showInputDialog (
    this, // parent component is the JFrame you are launching this from
    "Insert new Menu Name"
);

正如你所看到的,我在java文件中尝试了if语句,但是没有用。

我错过了什么?

3 个答案:

答案 0 :(得分:2)

问题

<强> if (toolbar == null)

<强>解决方案

使用!null检查代替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"

答案 1 :(得分:1)

删除 if(toolbar==null)仅当工具栏为空

时才会出现此情况

并制作

 if (toolbar != null){
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Vježbe");
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setElevation(0);
    }

答案 2 :(得分:1)

只需将此代码添加到onCreate事件中,如下所示:

@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

}