导航抽屉活动无法从资源

时间:2015-12-11 14:24:57

标签: android android-layout android-fragments android-navigation-drawer

所以我尝试根据this教程为我的应用创建导航抽屉,以及我尝试分配的所有资源,根据指南已经准备好了在Android Studio中实现,只是没有出现在代码完成中。

这是一段代码,我可以自动完成我的资源,顺便说一下,我下载了整个代码并在其布局中搜索,但没有找到。

在此方法中,找不到action_websearch:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

2 个答案:

答案 0 :(得分:0)

该ID是菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_websearch"
          android:icon="@drawable/action_search"
          android:title="@string/action_websearch"
          android:showAsAction="ifRoom|withText" />
</menu>

您可以在res / menu文件夹中找到它。

确保菜单布局中没有错误。

答案 1 :(得分:0)

如果您愿意,我可以共享您可以修改的代码源!

因为我可以给你一些帮助,但也许你会更加困惑!

Example DRAWER

// <强> MAINACTIVITY:

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private DrawerLayout drawerLayout;
    private ArrayAdapter<String> adapter;
    private ActionBarDrawerToggle drawerToggle;
    private String title;

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

        //Configuracion de las referencias de cada uno de los elementos
        listView = (ListView)findViewById(R.id.list_drawer);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        title = getTitle().toString();

        //Llamada al metodo configs
        configs();

        //Configuracion de el boton por default para llamar el drawer en el ActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    private void configs() {
        //Creacion y configuracion de la lista para el drawer
        String[] array = { "1", "2", "3", "4", "5" };
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
        listView.setAdapter(adapter);

        //Configuracion del listener de los elementos de la lista
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });

        //Referencia y creacion del drawerToogle pasando como referencia el layout y los strings
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {

            //Metodo llamado cuadno se abre el drawer
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Open!");
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(title);
            }
        };

        //Configuracion del listener para habilitar el drawer
        drawerToggle.setDrawerIndicatorEnabled(true);
        drawerLayout.setDrawerListener(drawerToggle);
    }

    //Actualiza el estado del navigation drawer
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    //Mantiene el estado del navigation drawer durante una configuracion de la actividad (Voltear el telefono)
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        //Activa el toogle del navigation drawer
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

// <强> layout.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The first child in the layout is for the main Activity UI-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Hello World!"
            android:gravity="center"
            android:id="@+id/textView" />

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <ListView
        android:id="@+id/list_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"/>

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