前言:
作用:导航控件---显示标题、导航back、快捷操作、菜单等。而且Toolbar不一定要放在顶部,也可以放底部。
使用:
1.引入support-v7包
2.修改主题: <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
注意主题一定要使用:NoActionBar
3.写布局,把Toolbar当做一个普通的容器使用。
4.Activity--->AppcompatActivity
5.使用Toolbar替换ActionBar
setSupportActionBar(toolbar);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res/com.ricky.materialdesign.toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:title="网易新闻"
app:titleTextColor="#ff0"
app:subtitle="旅行"
app:logo="@drawable/ic_launcher"
app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" //让标题居中显示在toobar
android:text="textview"
/>
</android.support.v7.widget.Toolbar>
</LinearLayout>
设置NavigationIcon的点击事件监听,比如返回按钮。
//app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
finish();
}
});
Toolbar设置SearchView
1.将Toolbar设置为ActionBar
setSupportActionBar(toolbar);
2.编写menu文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ricky.materialdesign.toolbar.MainActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:orderInCategory="100"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
android:title="查找"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="设置"/>
<item
android:id="@+id/action_share"
android:orderInCategory="100"
app:showAsAction="always"
android:title="分享"
android:icon="@android:drawable/ic_menu_share"/>
<item
android:id="@+id/action_edit"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:title="编辑"
android:icon="@android:drawable/ic_menu_edit"/>
</menu>
3.重写Activity的onCreateOptionsMenu和onOptionsItemSelected方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//SearchView在Menu里面
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
//设置一出来就直接呈现搜索框---SearchView
// searchView.setIconified(false);
//进来就呈现搜索框并且不能被隐藏
// searchView.setIconifiedByDefault(false);
//有时候我们需要实现自定义扩展效果
//通过猜想,searchView用到了一个布局,去appcompat里面找到abc_search_view.xml,该里面的控件的属性
ImageView icon = (ImageView) searchView.findViewById(R.id.search_go_btn);
icon.setImageResource(R.drawable.abc_ic_voice_search_api_mtrl_alpha);
icon.setVisibility(View.VISIBLE);
// searchView.setMaxWidth(200);
SearchView.SearchAutoComplete et = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
et.setHint("输入商品名或首字母");
et.setHintTextColor(Color.WHITE);
//设置提交按钮是否可用(可见)
searchView.setSubmitButtonEnabled(true);
// icon.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "提交", 1).show();
// }
// });
//作业:像AutoCompleteTextView一样使用提示
// searchView.setSuggestionsAdapter(adapter)
//监听焦点改变
searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
});
//searchView的关闭监听
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
// TODO Auto-generated method stub
return false;
}
});
searchView.setOnSearchClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "提交", 0).show();
}
});
//监听文本变化,调用查询
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
//提交文本
Toast.makeText(MainActivity.this, "提交文本:"+text, 0).show();
return false;
}
@Override
public boolean onQueryTextChange(String text) {
// 文本改变的时候回调
System.out.println("文本变化~~~~~"+text);
return false;
}
});
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Toolbar滑动半透明效果
思路:监听ScrollView的滑动事件,不断地修改Toolbar透明度
步骤:
1.定义回调接口
public interface TranslucentListener {
/**
* 透明度的回调监听
* @param alpha 0~1 透明度
*/
public void onTranlucent(float alpha);
}
2.定义自定义的ScrollView
public class MyScrollView extends ScrollView {
private TranslucentListener listener;
public void setTranslucentListener(TranslucentListener listener) {
this.listener = listener;
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(listener!=null){
int scrollY = getScrollY();
int screen_height = getContext().getResources().getDisplayMetrics().heightPixels;
if(scrollY<=screen_height/3f){//0~1f,而透明度应该是1~0f
listener.onTranlucent(1-scrollY/(screen_height/3f));//alpha=滑出去的高
度/(screen_height/3f)
}
}
}
}
3.编写布局文件
<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"
>
<com.ricky.materialdesign.translucentscrolltoolbar.MyScrollView
android:id="@+id/scrollView"
android:clipToPadding="false"
android:clipChildren="false"
android:paddingTop="?attr/actionBarSize"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button4" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</android.support.v7.widget.LinearLayoutCompat>
</com.ricky.materialdesign.translucentscrolltoolbar.MyScrollView>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="sun"
android:background="?attr/colorPrimary"
>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
android:clipToPadding="false" 该控件的绘制范围是否不在Padding里面。false:绘制的时候范围会考虑padding即会往里面缩进。
android:clipChildren="false" 子控件是否能不超出padding的区域(比如ScrollView上滑动的时候,child可以滑出该区域)
4.在Acriviry中监听回调,设置透明度
@Override
public void onTranlucent(float alpha) {
toolbar.setAlpha(alpha);
}
网友评论