簡體   English   中英

導航抽屜圖標(ic_drawer)未顯示

[英]Navigation Drawer Icon (ic_drawer) not showing

我正在為我的應用實現導航抽屜。 現在除了一個小故障外它完美無缺。 當我設置導航抽屜圖標(ic_drawer)來替換常規的“HomeAsUp”插入符號圖標時,我仍然得到箭頭。 導航抽屜圖標不顯示。 我已經實現了android開發者網站上的每個方法。 但它似乎沒有用。

以下是我的代碼:

DrawerLayout mDrawerLayout;
FrameLayout leftDrawer, rightDrawer, contentFrame;
ActionBarDrawerToggle mDrawerToggle;

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

private void initializeViews() {
    // TODO Auto-generated method stub
    mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout, R.drawable.ic_drawer,
            R.string.drawer_open_content_desc,
            R.string.drawer_close_content_desc);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    leftDrawer = (FrameLayout) findViewById(R.id.drawerLeft_frame);
    rightDrawer = (FrameLayout) findViewById(R.id.drawerRight_frame);
    contentFrame = (FrameLayout) findViewById(R.id.content_frame);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

我知道現在回答這個問題已經很晚了,但這對任何人都有幫助。

您應該添加這些代碼行來顯示導航圖標。

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

只需將此代碼放在styles.xml文件中:

<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>

我有同樣的問題,這對我有用。

編輯

以編程方式,您可以設置: getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

你必須把這段代碼:

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

要么

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

我希望這段代碼可以幫到你。

我通過為我的活動指定<meta-data>解決了同樣的問題,但android:value指向同一個Activity。

因此,如果將活動名稱稱為MainActivity,則將以下標記添加到清單文件中的活動。

<meta-data
     android:name="android.support.PARENT_ACTIVITY"
     android:value="com.example.MainActivity" />

希望這可以幫助。 :)

檢查AndroidManifest.xml中的特定活動, 不要設置元數據"android.support.PARENT_ACTIVITY" 如果設置如下,請嘗試刪除<meta-data>

<activity> 
...
 <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.company.project.ParentActivity"/>
</activity>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM