Android导航抽屉/ DrawerLayout ...切换无法正常工作

时间:2014-03-15 00:20:34

标签: java android navigation action drawer

我不明白,我仍然遵循每个教程仍然遇到同样的问题。 在某些设备中,我将ic_drawer(3行icone)作为打开菜单的按钮,另一方面;大多数其他设备我得到了up caret(箭头)icone。我真的不明白这个问题是什么。

以下是代码:

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.DrawerLayout);
    ListView     menu = (ListView)findViewById(R.id.Menu);
    String[]    listElements = getResources().getStringArray(R.array.listElements);
    ActionBarToggle toggle = new ActionBarDrawerToggle(
                    this, drawer,
                    R.drawable.ic_drawer,
                    R.string.open,
                    R.string.close){

                public void onDrawerClosed(View v){
                    super.onDrawerClosed(v);
                    getActionBar().setTitle(title);
                    invalidateOptionsMenu();
                }

                public void onDrawerOpened(View v){
                    super.onDrawerOpened(v);
                    getActionBar().setTitle("Menu de l'application");
                    invalidateOptionsMenu();
                }
            };
            drawer.setDrawerListener(toggle);
            getActionBar().setDisplayHomeAsUpEnabled(true); 
            getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    toggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (toggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

抽屉!

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <include
        android:id="@+id/ContenuPrincipal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/activity_main_relative"
        /> 

    <!-- ListView... La liste des options du menu -->
       <ListView
            android:id="@+id/Menu"
            android:layout_width="250dp"
            android:layout_height="fill_parent"
            android:choiceMode="singleChoice"
            android:layout_gravity="start"
            android:background="#333"
            android:divider="#666"
            android:dividerHeight="1dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            />  


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

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.usthb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="app.usthb.MainActivity"
            android:label="@string/app_name" >
        </activity>

         <activity android:name="Acceuil"
             android:label="@string/app_name"
             android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>

         <activity android:name="Ecran2"
                    android:label="@string/Ecran2">

         </activity>


    </application>

</manifest>

嗯,这就是代码,就像我说的那样;在某些设备(如我的模拟器)上完美运行,但在其他设备(如我的S3)中,我只需要使用Up Caret而不是3行icone。我真的会帮助你。谢谢。

1 个答案:

答案 0 :(得分:0)

将ActionBar设置为不作为Home 用:

  getActionBar().setDisplayHomeAsUpEnabled(false);