简体   繁体   English

如何在所有活动中添加导航抽屉

[英]How to Add Navigation Drawer in all Activities

I have a navigation Drawer in my App.Now i need to make that drawer appear in all my activities . 我的App中有一个navigation Drawer 。现在我需要使该抽屉出现在我的所有activities

I saw many questions similar to this & found a Solution https://stackoverflow.com/a/4922740/2781359 我看到许多与此类似的问题并找到了解决方案https://stackoverflow.com/a/4922740/2781359

But I tried it in my App.Then it throws an Exception . 但是我在App中尝试了,然后抛出Exception

So Help me in the Right Direction :) 因此,请按正确的方向帮助我:)

Thanks for your Help ... 谢谢你的帮助 ...
Logcat logcat的

       01-25 22:35:14.062: E/AndroidRuntime(17091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ControllerDroid.client/com.ControllerDroid.client.activity.ControlActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.os.Looper.loop(Looper.java:136)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.main(ActivityThread.java:5081)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at java.lang.reflect.Method.invoke(Method.java:515)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at dalvik.system.NativeStart.main(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.widget.AdapterView.addView(AdapterView.java:478)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.MainActivity.setContentView(MainActivity.java:37)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.MainActivity.onCreate(MainActivity.java:56)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.ControlActivity.onCreate(ControlActivity.java:62)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.Activity.performCreate(Activity.java:5231)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-25 22:35:14.062: E/AndroidRuntime(17091):    ... 11 more

MainActivity.java MainActivity.java

    public class MainActivity extends ActionBarActivity
{
    protected DrawerLayout fullLayout;
    protected ListView actContent;
    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    @Override
    public void setContentView(final int layoutResID)
    {
       fullLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main_drawer, null); // Your  base layout here
        ListView actContent = (ListView) fullLayout.findViewById(R.id.drawer);
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting    the    content  of layout your  provided  to the  act_content frame
        super.setContentView(fullLayout);
        // here you can get your drawer buttons and define how they should
        // behave and what must they do, so you won't be needing to repeat it in
        // every activity class
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);


                // set a custom shadow that overlays the main content when the drawer
                // opens
                fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, fullLayout);
                fullLayout.setDrawerListener(mDrawerToggle);
    }


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

activity_main_drawer.xml activity_main_drawer.xml

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

    <!-- The main content view -->

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color"
            android:text="@string/drawer_text" />
    </RelativeLayout>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:cacheColorHint="#00000000" 
         android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:textAppearance="@android:style/TextAppearance.Medium"
         android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

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

Your MainActivity is abstract and cannot be instantiated. 您的MainActivityabstract ,无法实例化。

If you intend to keep it abstract, remove its entry from the manifest and make sure you're not trying to instantiate with an explicit Intent . 如果您打算使其抽象,请从清单中删除其条目,并确保您未尝试使用显式的Intent实例化。

You are casting the wrong layout id's 您投放了错误的布局ID

Try this 尝试这个

public abstract class MainActivity extends ActionBarActivity
{
    protected DrawerLayout fullLayout;
    protected ListView actContent;
    private ListView mDrawerList;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    @Override
    public void setContentView(final int layoutResID)
    {
       fullLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main_drawer, null); // Your  base layout here
        ListView actContent = (ListView) fullLayout.findViewById(R.id.drawer);
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting    the    content  of layout your  provided  to the  act_content frame
        super.setContentView(fullLayout);
        // here you can get your drawer buttons and define how they should
        // behave and what must they do, so you won't be needing to repeat it in
        // every activity class
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);


                // set a custom shadow that overlays the main content when the drawer
                // opens
                fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, fullLayout);
                fullLayout.setDrawerListener(mDrawerToggle);
    }

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

Edited Full Solution : 编辑完整解决方案:

main_content.xml main_content.xml

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

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

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


    <RelativeLayout
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <ListView
            android:layout_below="@+id/drawer_list"
            android:layout_width="240dp"
            android:id="@+id/filters_list"
            android:layout_height="wrap_content"/>
    </RelativeLayout>       
</android.support.v4.widget.DrawerLayout>

BaseActivity BaseActivity

protected DrawerLayout fullLayout;
protected FrameLayout actContent;
private ActionBarDrawerToggle mDrawerToggle;
ListView mDrawerList;


@Override
public void setContentView(final int layoutResID) {


    fullLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.main_content, null); // Your base layout here


    mDrawerList = (ListView)fullLayout.findViewById(R.id.drawer_list);
    fullLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,mFilterList);
    fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    int drawer_icon = R.drawable.ic_drawer;   
    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            fullLayout,
           drawer_icon,
            R.string.drawer_open,
         R.string.drawer_close
        ) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    fullLayout.setDrawerListener(mDrawerToggle);

    actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
    getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame

    super.setContentView(fullLayout);
}

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

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM