Toolbar+DrawerLayout
Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,
以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容,
使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,
Google也留给了开发者很多可定制修改的余地,
这些可定制修改的属性在API文档中都有详细介绍,如:
1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏;
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值;
8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Day03.ToolBarTestActivity">
<android.support.v7.widget.Toolbar
app:navigationIcon="@drawable/ic_launcher_background"
app:subtitleTextColor="#4f5"
app:subtitle="副标题"
app:titleTextColor="#f40"
app:title="标题"
app:logo="@mipmap/ic_launcher_round"
android:background="#f95"
android:id="@+id/tb"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:layout_below="@id/tb"
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#65f"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是主界面" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_gravity="left"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="左侧视图"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Menu文件下的Menu布局
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/item1" android:title="菜单" app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/subMenu" android:title="子菜单" app:showAsAction="ifRoom"></item>
</menu>
</item>
</menu>
package com.example.sixeightwork.Day03;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sixeightwork.R;
public class ToolBarTestActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tool_bar_test);
initview();
initDarerLayout();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public void initview(){
textView=findViewById(R.id.textv);
toolbar=findViewById(R.id.tb);
drawerLayout=findViewById(R.id.dl);
setSupportActionBar(toolbar);//让他替换actionbar
//导航按钮添加点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
drawerLayout.closeDrawer(Gravity.LEFT);
}else{
drawerLayout.openDrawer(Gravity.LEFT);
}
}
});
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ToolBarTestActivity.this, "点击了导航按钮", Toast.LENGTH_SHORT).show();
}
});
}
public void initDarerLayout(){
//绑定toolbar和drawerLayout
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openlayout,R.string.closelayout);
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
}
}