您的位置:首页 > 移动开发 > Android开发

ToolBar的使用

2016-03-14 14:07 323 查看
看视频学习了actionBar的用法在查阅资料的时候发现Google在2013年就推出ToolBar取代actionBar的时候,内心是奔溃的,只好在花一些时间学习ToolBar的使用,虽然学习了过时的actionBar但是ToolBar还是有很多和actionBar差不多的,可以借鉴的学习ToolBar

在学习ToolBar的时候查阅很多资料都是AS的早期版本,使用挺麻烦的,但是在AS.2.0中已经帮你做好了这一切,只需要通过修改style(样式),layout(布局),java(代码)就能简简单单的使用这个控件(没错,ToolBar就是一个控件,学习的时候没把它当控件使用,导致走了很多弯路,当回过头来,把它想成一个控件来使用【确确实实就是一个控件】,会变得很简单)

样式

<resources>

<!-- Base application theme. -->
<!--  在这里可以修改ToolBar的样式,创建style然后在TooLBar控件中使用-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<!-- 菜单弹出样式  这里自己修改成黑色-->
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat" >
<item name="android:colorBackground">@color/colorPrimary</item>
</style>

</resources>

布局

1.ToolBar的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.example.myapplication.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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" >

<ImageView
android:src="@drawable/ic_search_black_48dp"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize" />
</android.support.v7.widget.Toolbar>
<!--popupTheme  菜单弹出的样式
android:layout_height="?attr/actionBarSize" ToolBar的高度
ImageView在ToolBar中自定义一个ImageView
-->
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

2.菜单的布局

<menu 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"
tools:context="com.example.myapplication.MainActivity" >
<item android:id="@+id/action_settings"
android:title="发起群聊"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_addFreends"
android:title="添加朋友"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_scan"
android:title="扫一扫"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_getPayMoney"
android:title="收付款"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_helpAndReturn"
android:title="帮助与反馈"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

代码

1.找到这个控件然后可以设置一系列东西(看需求 图标 主标题 副标题 向上按钮等等)

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

2.菜单的加载和监听点击事件

@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);
}


background和src的区别
background会根据ImageView组件的大小进行拉伸
src存放的就是原图的大小
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android