当我尝试在手机上安装我的应用程序进行测试时,我得到的DrawerLayout无法投出错误。这只是一个基本的滑出式抽屉。我是新手,所以你能做的任何事情都会很棒!
这是我的主要活动:
public class MainActivity extends Activity {
private FragmentNavigationDrawer dlDrawer;
public final static String EXTRA_MESSAGE = "Nothing";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find our drawer view
dlDrawer = (FragmentNavigationDrawer) findViewById(R.id.drawer_layout);
// Setup drawer view
dlDrawer.setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer),
R.layout.drawer_list_item, R.id.flContent);
// Add nav items
// dlDrawer.addNavItem("First", "Text Note", TextNoteFragment.class);
//dlDrawer.addNavItem("Second", "Second Fragment", SecondFragment.class);
//dlDrawer.addNavItem("Third", "Third Fragment", ThirdFragment.class);
// Select default
if (savedInstanceState == null) {
dlDrawer.selectDrawerItem(0);
}
}
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
if (dlDrawer.isDrawerOpen()) {
menu.findItem(R.id.action_search).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
if (dlDrawer.getDrawerToggle().onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
case R.id.help_setting:
openHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
dlDrawer.getDrawerToggle().syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
dlDrawer.getDrawerToggle().onConfigurationChanged(newConfig);
}
public void openHelp() {
Intent intent = new Intent(this, HelpActivity.class);
String message = "Will you just shut up already? It's not that hard to figure out.";
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
public void openSearch() {
//Do something here.
}
public void openSettings() {
Intent intent = new Intent(this,SettingsClass.class);
startActivity(intent);
}
}
这是我的主要活动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 -->
<!-- The navigation drawer -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/lvDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="0dp"
android:background="@android:color/background_light"
/>
</android.support.v4.widget.DrawerLayout>