2

まったく新しいAndroidプロジェクトを作成し、それにSherlockActionBarとSlidingMenuを追加しました。

サンプルファイルからすべてをコピーしたと思いましたが、それでも機能しません。メインのフラグメントが表示されますが、アイコンをクリックしてもメニューがまったく表示されません。

何が足りないの?

これがクラスとXMLです。これらのclasses/xmlファイルを除いて、プロジェクトは空です。

クラス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);
    }
}

クラス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);
    }
}

クラス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 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 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 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 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" />

マニフェスト:

<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はサンプルプロジェクトからコピーされます。

4

1 に答える 1

1

jfeinstein10に質問したところ、onOptionsItemSelectedメソッドを追加するのを忘れたことがわかりました。

開くコードを修正します(上記のクラスを修正するため):

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

onCreateの後にMainActivityクラスにある必要があります。

于 2012-12-20T19:38:45.470 に答える