简体   繁体   English

试图获得一个全新的android项目与SlidingMenu(jfeinstein10)和ActionbarSherlock一起使用

[英]Trying to get a brand new android project to work with SlidingMenu (jfeinstein10) and ActionbarSherlock

I created a brand new android project and added SherlockActionBar and SlidingMenu to it. 我创建了一个全新的android项目,并向其中添加了SherlockActionBar和SlidingMenu。

I thought that i had copied everything from the example file but it still doesn't work. 我以为我已经复制了示例文件中的所有内容,但仍然无法正常工作。 It shows my main fragment but the menu doesn't show at all when i click on the icon. 它显示了我的主要片段,但是当我单击图标时菜单根本不显示。

What am i missing?? 我想念什么?

Here are the classes and XML. 这是类和XML。 Project is empty except for these classes/xml files. 除这些类/ xml文件外,项目为空。

Class 1 (Main activity): 第1类(主要活动):

import android.os.Bundle;   
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class Main extends SlidingFragmentActivity {

    protected MenuFragment mFrag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFrag = new MenuFragment();

        setContentView(R.layout.activity_main);

        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, new SectionOneFragment())
        .commit();

        //  set the Behind View
        setBehindContentView(R.layout.activity_menu);

        getSupportFragmentManager()
        .beginTransaction()         
        .replace(R.id.menu_frame, mFrag)
        .commit();

        // customize the SlidingMenu
        SlidingMenu sm = getSlidingMenu();
        sm.setMode(SlidingMenu.LEFT);
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setSlidingActionBarEnabled(false);
    }
}

Class 2 (Menu fragment): 第2类(菜单片段):

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MenuFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_menu, null);
    }
}

Class 3 (SectionOne fragment) 第3类(SectionOne片段)

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SectionOneFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_main, null);
    }
}

Xml 1 (res->layout->activity_main.xml): Xml 1(res-> layout-> activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Xml 2 (res->layout->activity_menu.xml): Xml 2(res-> layout-> activity_menu.xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Xml 3 (res->layout->fragment_main.xml): Xml 3(res-> layout-> fragment_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<TextView 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:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:textColor="#ff00ff"
tools:context=".Main" />

Xml 4 (res->layout->fragment_menu.xml): Xml 4(res-> layout-> fragment_menu.xml):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TEST"
android:textColor="#00ff00" />

Manifest: 表现:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Shadow.xml, dimens.xml are copied from the sample project. 从示例项目复制了Shadow.xml,dimens.xml。

Asked jfeinstein10 and i see that i forgot to add the onOptionsItemSelected method. 问jfeinstein10,我看到我忘了添加onOptionsItemSelected方法。

Correct code to open (to fix my class above): 更正要打开的代码(以修复上面的类):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;        
    }
    return super.onOptionsItemSelected(item);
}

Should be in the MainActivity class after onCreate. 应该在onCreate之后在MainActivity类中。

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

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