Toolbar toolbar;
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉系统自带的actionBar
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
//设置自定义的actionBar
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openLayout, R.string.closeLayout);
toggle.syncState();
drawer.addDrawerListener(toggle);
}
/**
*封装控件
*/
public void initView() {
toolbar = findViewById(R.id.toolbar);
drawer = findViewById(R.id.drawer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);//填充菜单
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.subMenu) {
Toast.makeText(this, "点击了子菜单", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "点击了主菜单", Toast.LENGTH_SHORT).show();
if (drawer.isDrawerOpen(Gravity.LEFT)){
drawer.closeDrawer(Gravity.LEFT);
}
}
return super.onOptionsItemSelected(item);
}
}
파일 메뉴 (여기에 두 개의 메뉴 버튼을 추가)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu1"
android:icon="@mipmap/ic_launcher"
android:title="菜单"
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/subMenu"
android:title="子菜单"
app:showAsAction="ifRoom"></item>
</menu>
</item>
</menu>
레이아웃 파일
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
app:logo="@mipmap/ic_launcher"
app:title="标题"
app:subtitle="副标题"
app:navigationIcon="@android:drawable/btn_plus">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="6666666666"
android:drawableLeft="@android:drawable/alert_light_frame"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你看不见我"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
A, 도구 모음의
역할 : 이전 시스템을 제거하기 위해 사용하는, 또는 액션 바 할은 액션 바 온다
:. 1 수정 매니페스트 파일 안드로이드 액션 바하기 : 테마 = "@ 스타일 / Theme.AppCompat.Light.NoActionBar"
2. 한 페이지를 다음에서 onCreate를 방법 : requestWindowFeture (Window.FEAUTURE_NO_TITLE)
대신의 액션 바 : setSupportActionBar (툴바 오브젝트);
사용법 :
1 제목 setTitle이라는 ()를 추가,
2 자막 setSubTitle을 ()을 첨가 하였다
. 3 로고 ()를 추가, setLogo를 ();
. 4 개 추가 탐색 버튼 setNavigationIcon (R.mipmap.xx을)
. 5 추가 내비게이션 모니터 : 도구 모음 : setNavigationOnClickListener (재기록 온 클릭 ());