我已经在这个网站上查看过多个问题及其答案,但这些导航抽屉似乎都不像我做的那样。
public class Main extends Activity {
// Navigation Drawer variables
private String[] navMenu = new String[] { "Home", "Menu", "Settings" };
private DrawerLayout navDrawer;
private ListView navList;
private ArrayAdapter<String> navAdapter;
// Fragments
private Home fHome = new Home();
private Menu fMenu = new Menu();
private Settings fSettings = new Settings();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationDrawer();// Navigation Drawer setup
}
private void navigationDrawer() {
navDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navList = (ListView) findViewById(R.id.left_drawer);
navAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, navMenu);
navList.setBackgroundColor(Color.parseColor("#1c2d4b"));
navList.setAdapter(navAdapter);
navList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
navDrawer.closeDrawers();
//TODO Switch current fragment
//navMenu[position]
}
});
}
}
activity_main.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">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
上面是我的Main和它的xml布局,如何在Menu
下拉列表中创建navMenu
项?我使用导航抽屉相当新,所以尽管它运行良好,但我不确定我是否以正确的方式完成了它。