首先创建 几个文件
- Vaules-v22文件夹 里面有一个styles.xml
- menu文件夹 meun下有main.xml
然后在建一个toolbar布局
image.png
创建一个values-v22文件夹
values-v22文件加载中的
styles.xml **********
···
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">@android:color/holo_blue_bright</item>
<item name="colorPrimaryDark">@android:color/holo_blue_bright</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Light" >
<item name="android:colorBackground">@color/colorPrimaryDark</item>
<item name="android:textColor">@android:color/white</item>
<item name="overlapAnchor">false</item>
</style>
</resources>
···
menu文件夹中
main.xml
···
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="语文"
android:icon="@mipmap/ic_launcher"
android:id="@+id/yuwen"
/>
<item android:title="数学"
android:icon="@mipmap/ic_launcher"
android:id="@+id/shuxue"/>
<item android:title="英语"
android:icon="@mipmap/ic_launcher"
android:id="@+id/yingyu"/>
</menu>
···
mainActivity.xml
···
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<include
layout="@layout/toolbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
···
************toolbarlLayout.xml*************
···
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试专用"
android:id="@+id/text"/>
</android.support.v7.widget.Toolbar>
···
--------------------MainAcivity中-----------
···
package com.example.toobar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//设置导航图标
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击导航图标", Toast.LENGTH_SHORT).show();
}
});
//设置logo
toolbar.setLogo(R.mipmap.ic_launcher_round);
//这两个监听不是针对于logo的 是toolbar的全局监听
toolbar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击logo", Toast.LENGTH_SHORT).show();
}
});
toolbar.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(MainActivity.this, "长按logo", Toast.LENGTH_SHORT).show();
return false;
}
});
//设置主副标题
toolbar.setTitle("主标题");
toolbar.setSubtitle("副标题");
//设置溢出菜单
//自定义样式必须在加载选项之前
toolbar.setPopupTheme(R.style.PopupMenu);
toolbar.inflateMenu(R.menu.main);
toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.yuwen:
Toast.makeText(MainActivity.this, "语文", Toast.LENGTH_SHORT).show();
break;
case R.id.shuxue:
Toast.makeText(MainActivity.this, "数学", Toast.LENGTH_SHORT).show();
break;
case R.id.yingyu:
Toast.makeText(MainActivity.this, "英语", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
textView = (TextView) findViewById(R.id.text);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击TextView", Toast.LENGTH_SHORT).show();
}
});
}
}
···
网友评论