我會實施與supportv7庫的導航抽屜,所以我將使用該應用程序甚至2.3 Android ..我不能這樣做..我遵循一些例子,但仍然有錯誤..這是我到目前爲止寫的:不能修改導航抽屜使用supportv7
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Customizzo la actionbar */
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbarcustom);
setTitle("Hi");
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(Color.WHITE);
}
}
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
dataList.add(new DrawerItem("Hi All")); // adding a header to the list
dataList.add(new DrawerItem("News", R.drawable.news));
dataList.add(new DrawerItem("Second Tab", R.drawable.arrow));
dataList.add(new DrawerItem("Third tab", R.drawable.high_importance));
dataList.add(new DrawerItem("4 tab", R.drawable.star));
dataList.add(new DrawerItem("5 Tab", R.drawable.spam));
dataList.add(new DrawerItem("6 Tab", R.drawable.group));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
if (dataList.get(0).isSpinner()
& dataList.get(1).getTitle() != null) {
SelectItem(2);
} else if (dataList.get(0).getTitle() != null) {
SelectItem(1);
} else {
SelectItem(0);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 1:
fragment = new FragmentThree();
break;
case 2:
fragment = new FragmentOne();
break;
case 3:
fragment = new FragmentOne();
break;
case 4:
fragment = new FragmentOne();
break;
case 5:
fragment = new FragmentOne();
break;
case 6:
fragment = new FragmentTwo();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction ft = frgManager.beginTransaction();
ft.replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()){
case R.id.action_settings:
Intent i = new Intent(MainActivity.this, settings.class);
startActivity(i);
return true;
}
return false;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (dataList.get(position).getTitle() == null) {
SelectItem(position);
}
}
}
}
這些都是進口:
import android.app.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.os.*;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.*;
import android.support.v4.widget.DrawerLayout;
import android.view.*;
import android.widget.*;
import com.readystatesoftware.systembartint.*;
import java.util.*;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.*;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
的錯誤是在這些線路:
fragment.setArguments(args);
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction ft = frgManager.beginTransaction();
ft.replace(R.id.content_frame, fragment)
.commit();
似乎該方法是不適用於android.app.Fragment ..我試圖在每一個方面沒有成功..是否還有別的東西要改變?
您需要使用'android.support.v4.app.Fragment',而不是'android.app.Fragment'。 – EpicPandaForce 2014-09-03 14:06:25