赞
踩
原创文章,如有转载,请注明出处:http://blog.csdn.net/myth13141314/article/details/64129804
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl_drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header" //抽屉的头部布局
app:menu="@menu/menu_main_drawer" /> //抽屉的菜单布局
</android.support.v4.widget.DrawerLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="172dp"
android:background="@color/colorPrimary"
android:clickable="true"
>
<ImageView
android:id="@+id/header_background"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
android:layout_marginBottom="18dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my application"
android:textColor="@color/white"
android:textSize="14dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"/>
</RelativeLayout>
</FrameLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_home"
android:title="@string/menu_home"
android:icon="@mipmap/menu_home"
/>
<item
android:id="@+id/menu_download"
android:title="@string/menu_download"
android:icon="@mipmap/menu_download"
/>
<item
android:id="@+id/menu_bookmark"
android:title="@string/menu_bookmark"
android:icon="@mipmap/menu_bookmark"/>
<item
android:id="@+id/menu_setting"
android:title="@string/menu_setting"
android:icon="@mipmap/menu_setting"/>
<item
android:id="@+id/menu_like"
android:title="@string/menu_like"
android:icon="@mipmap/menu_like"/>
<item
android:id="@+id/menu_share"
android:title="@string/menu_share"
android:icon="@mipmap/menu_share"/>
<item
android:id="@+id/menu_feedback"
android:title="@string/menu_feedback"
android:icon="@mipmap/menu_feedback"/>
<item
android:id="@+id/menu_update"
android:title="@string/menu_update"
android:titleCondensed="V1.1"
android:icon="@mipmap/menu_update"/>
<item
android:id="@+id/menu_exit"
android:title="@string/menu_exit"
android:icon="@mipmap/menu_exit"/>
</group>
</menu>
drawerLayout = (DrawerLayout) findViewById(R.id.dl_drawerlayout);
//Drawerlayout 和 toolbar绑定
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
//抽屉菜单布局的初始化以及设置点击监听
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//id 为menu布局文件里的id
switch(item.getItemId()) {
case R.id.menu_home:
break;
case R.id.menu_download:
break;
case R.id.menu_bookmark:
break;
case R.id.menu_setting:
break;
case R.id.menu_like:
break;
case R.id.menu_share:
break;
case R.id.menu_feedback:
break;
case R.id.menu_update:
break;
case R.id.menu_exit:
break;
}
return true;
}
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initDrawerLayout();
}
private void initDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.dl_drawerlayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_home:
break;
case R.id.menu_download:
break;
case R.id.menu_bookmark:
break;
case R.id.menu_setting:
break;
case R.id.menu_like:
break;
case R.id.menu_share:
break;
case R.id.menu_feedback:
break;
case R.id.menu_update:
break;
case R.id.menu_exit:
break;
}
return true;
}
@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。