这是MainActivity.java
public class MainActivity extends ActionBarActivity {
// inisiasi toolbar
private Toolbar toolbar;
// navigation drawer
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
String title = "My App";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
if (toolbar != null) {
toolbar.setTitle(title);
setSupportActionBar(toolbar);
}
initDrawer();
}
/**
* init navigation drawer thing
*/
private void initDrawer() {
//setup navigation drawer
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.txt_open, R.string.txt_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// when drawer closed
toolbar.setTitle(title);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// when drawer open
toolbar.setTitle("Nav menu");
}
};
// setDrawerlisterner
drawerLayout.setDrawerListener(drawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (drawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}
这里是xml文件布局视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Toolbar -->
<include layout="@layout/toolbar" />
<!-- content -->
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Our Content"
android:layout_height="wrap_content" />
</RelativeLayout>
<!-- nav drawer -->
<LinearLayout
android:layout_gravity="start"
android:orientation="vertical"
android:background="#fff"
android:layout_width="280dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Nav drawer"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
有谁知道如何将listview添加到导航抽屉。我想在用户选择导航抽屉中的每个项目到片段时加载网页。我试图从一个示例中实现一个代码,但却出现了很多错误。
答案 0 :(得分:0)
好吧,没有什么可担心的NavigationDrawer。起初我们可能认为这是一种特殊的活动,这就是那个。但它太简单了。
第1步 - 创建包含2个布局的xml文件
<FrameLayout>
//main screen contents
</FrameLayout>
<Linear/Relative/ListView/xyz>
// any layout that u want to be shown in navigation drawer
</Linear/Relative/ListView/xyz>
第2步 - 使用几个代码
在活动类中定义您的抽屉drawerLayout = (DrawerLayout) findViewById(R.id.NvDrawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(drawerToggle);
并覆盖此方法
@Override
public void onDrawerSlide(View view, float v) {
}
@Override
public void onDrawerOpened(View view) {
}
@Override
public void onDrawerClosed(View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
第3步 - 在onOptionsItemSelected
中添加此代码if (drawerToggle.onOptionsItemSelected(item)) {
return true;