2015-09-27 116 views
0

我有一個新的應用程序,實際上是我的第一個應用程序,爲此我想顯示導航抽屜。不顯示操作欄

該應用運行正常,並在用戶登錄到遠程服務器後顯示用戶配置文件。

抽屜工作正常,打開和關閉如預期。

但應該有一個操作欄。而我沒有看到它。此外,漢堡包抽屜圖標也沒有被看到。

被加載細,顯示所述用戶簡檔數據的用戶簡檔類:

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顯示工具欄不爲空:

E/permapp (3292): ========>> toolbar: [email protected] 

的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 /價值/ 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> 
+0

貴'BaseActivity'從'AppCompatActivity'延長?你從哪個主題延伸出來? – LordRaydenMK

+0

是的,它擴展了AppCompatActivity,我現在在問題中添加了這個。主題是Theme.AppCompat.Light.NoActionBar。 – Stephane

回答

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

這是你的佈局的相關部分。您的Toolbar和您的FrameLayout都在RelativeLayout之內。 FrameLayout只是覆蓋你的Toolbar,覆蓋它。 要麼更換RelativeLayoutLinearLayout或告訴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" /> 
+0

我嘗試了LinearLayout替換內容元素的FrameLayout,但它根本沒有改變。 – Stephane

+0

layout_below屬性未被識別。 – Stephane

+1

你應該用LinearLayout替代RelativeLayout,而不是FrameLayout。 – FWeigl