操作栏不会显示

时间:2015-09-27 17:33:38

标签: android navigation-drawer

我有一个新的应用程序,实际上我的第一个应用程序,我想显示导航抽屉。

应用程序运行正常并在用户登录远程服务器后显示用户配置文件。

抽屉工作正常,按预期打开和关闭。

但是应该有一个动作栏。而且我没有看到它。此外,也没有汉堡包抽屉图标。

正常加载的用户个人资料类,显示用户个人资料数据:

public class UserProfileActivity extends UserActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
    }

}

用户活动是所有用户活动的基类:

public class UserActivity extends BaseActivity {

    private String email;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);
        setupDrawer();
    }

}

最后是基础活动:

public class BaseActivity extends AppCompatActivity {

private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;    
private DrawerLayout baseLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkVersion();
}

@Override
public void setContentView(int layoutResID) {
    baseLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.base, null);
    FrameLayout contentLayout = (FrameLayout) baseLayout.findViewById(R.id.content);
    getLayoutInflater().inflate(layoutResID, contentLayout, true);
    super.setContentView(baseLayout);

    // Set a Toolbar to replace the ActionBar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (null != toolbar) {
        setSupportActionBar(toolbar);
    }
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true); 
    actionBar.setDisplayHomeAsUpEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(this, baseLayout, toolbar , R.string.drawer_open, R.string.drawer_close) { 
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            //getActionBar().setTitle(mTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            //getActionBar().setTitle(mDrawerTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 
    };
    baseLayout.setDrawerListener(drawerToggle);
    baseLayout.post(new Runnable() {
        @Override
        public void run() {
            drawerToggle.syncState();
        }
    });
}

adb logcat显示工具栏不为null:

E/permapp ( 3292): ========>> toolbar: android.support.v7.widget.Toolbar@41d7b398

base.xml布局是:

<android.support.v4.widget.DrawerLayout
    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:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >    

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu" 
        />

</android.support.v4.widget.DrawerLayout>

toolbar.xml布局是:

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

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

样式在两个文件中完成,res / values / styles.xml包含:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>

和res / values-v19 / styles.xml包含:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

我有以下依赖项:

<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>android</artifactId>
  <version>4.1.1.4</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>support-v4</artifactId>
  <version>23.0.1</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>appcompat-v7</artifactId>
  <version>23.0.0</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>design</artifactId>
  <version>23.0.0</version>
  <type>aar</type>
</dependency>

1 个答案:

答案 0 :(得分:1)

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

这是布局的相关部分。您的ToolbarFrameLayout都在RelativeLayout范围内。 FrameLayout只是覆盖在Toolbar上,覆盖它。 将RelativeLayout替换为LinearLayout或将FrameLayout置于工具栏下方(RelativeLayout内),如    

<include
    android:id="@+id/toolbarinclude"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<FrameLayout
    android:layout_below="@id/toolbarinclude"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />