BottomAppBar
按钮的item布局
<?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/title1"
android:icon="@drawable/add"
android:title="title1"
android:contentDescription="content_description_title1"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/title2"
android:icon="@drawable/one"
android:title="title2"
android:contentDescription="content_description_title2"
app:showAsAction="never"
/>
<item
android:id="@+id/title3"
android:icon="@drawable/three"
android:title="title3"
android:contentDescription="content_description_title3"
app:showAsAction="ifRoom"
/>
</menu>
主界面xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity2">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="100dp"
android:clipToPadding="false">
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/myBottomAppBar"
android:layout_width="match_parent"
app:fabCradleMargin="7dp"按钮和底部栏的margin
android:backgroundTint="@color/colorLightBlue"
android:layout_height="wrap_content"
app:elevation="10dp" app不是Android
app:fabCradleVerticalOffset="15dp"按钮和底部栏的垂直距离
app:fabCradleRoundedCornerRadius="30dp"BottomAppBar嵌入位置圆角半径
android:layout_gravity="bottom"
app:hideOnScroll="true" 滚动时隐藏
app:fabAlignmentMode="center" 按钮所处位置,可以是end 右边
app:navigationIcon="@mipmap/onei"导航窗口
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"跟随主题颜色
app:menu="@menu/bottom_app_bar"添加的menu
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/add"
android:backgroundTint="@color/colorLightGreen"
app:layout_anchor="@id/myBottomAppBar"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
代码修改Bar缺口
float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, getResources().getDisplayMetrics());
TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(size, true);
ShapePathModel shapePathModel = new ShapePathModel();
shapePathModel.setTopEdge(triangleEdgeTreatment);
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable(shapePathModel);
materialShapeDrawable.setTint(0XFFDDDDDD);
bottomAppBar.setBackground(materialShapeDrawable);
修改FloatingButton
<style name="ThemeOverlay.App.FloatingActionButton" parent="">
<item name="colorSecondary">@color/colorYellow</item>
<item name="colorOnSecondary">@color/colorGrey</item>
<item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>
</style>
<style name="Widget.App.FloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton">
<item name="materialThemeOverlay">@style/ThemeOverlay.App.FloatingActionButton</item>
</style>
定义一个style绘制
style="@style/Widget.App.FloatingActionButton"
定义主题
监听事件
bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity2.this,"setNavigationOnClickListener",Toast.LENGTH_SHORT).show();
}
});
导航按钮
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.title1:
Toast.makeText(MainActivity2.this,"title1",Toast.LENGTH_SHORT).show();
break;
case R.id.title2:
Toast.makeText(MainActivity2.this,"title2",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
局部按钮
TopAppBar
基本用法相似
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity2">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
app:liftOnScroll="true"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="title"
app:menu="@menu/top_app_bar"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:navigationIcon="@mipmap/onei"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="100dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:clipToPadding="false">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reecyclerview"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
需要一个AppBarLayout封装
其中app:layout_scrollFlags=“scroll|enterAlways|snap”
显示顶部应用程序栏在向上滚动时消失,在向下滚动时出现
使用这个Bar之前theme要改成
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
一开始生成的时候滑动布局的NestScroll会在APPBar之中嵌套,只需要加入一行代码即可
app:layout_behavior="@string/appbar_scrolling_view_behavior"
<com.google.android.material.appbar.CollapsingToolbarLayout
app:expandedTitleTextAppearance="@style/TextAppearance.App.CollapsingToolbar.Expanded"
app:collapsedTitleTextAppearance="@style/TextAppearance.App.CollapsingToolbar.Collapsed"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="title"
app:menu="@menu/top_app_bar"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:navigationIcon="@mipmap/onei"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
强调,放大title。这个时候的title不会正常显示,需要自定义style(另外新建一个xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextAppearance.App.CollapsingToolbar.Expanded" parent="TextAppearance.MaterialComponents.Headline5">
<item name="android:textColor">@color/colorGhostWhith</item>
</style>
<style name="TextAppearance.App.CollapsingToolbar.Collapsed" parent="TextAppearance.MaterialComponents.Headline6">
<item name="android:textColor">@color/colorGhostWhith</item>
</style>
</resources>
在style中绑定才能显示出titie,切记不能顺手就把toolbar的style给定义了
标题内置图片
<androidx.coordinatorlayout.widget.CoordinatorLayout
...
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
...
android:layout_height="152dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
...
android:fitsSystemWindows="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/media"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:contentDescription="@string/content_description_media"
/>
<com.google.android.material.appbar.MaterialToolbar
...
android:background="@android:color/transparent"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
同时还要在Manifes里面把style定义为
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
这样就获得了透明图片的大标题栏
可折叠标题栏
<androidx.coordinatorlayout.widget.CoordinatorLayout
...>
<com.google.android.material.appbar.AppBarLayout
...>
<com.google.android.material.appbar.CollapsingToolbarLayout
...
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:contentScrim="?attr/colorPrimary"
app:statusBarScrim="?attr/colorPrimaryVariant">
...
<com.google.android.material.appbar.MaterialToolbar
...
app:layout_collapseMode="pin"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
app:contentScrim="?attr/colorPrimary"
折叠系统栏
app:statusBarScrim="?attr/colorPrimaryVariant"
折叠toolbar
app:layout_scrollFlags=“scroll|exitUntilCollapsed|snap”
exitUntilCollapsed滚动玩折叠之后保留在界面上
scroll随一起滚动
Bottom Navigation
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_menu" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/page_1"
android:enabled="true"
android:icon="@drawable/add"
android:title="label_1"/>
<item
android:id="@+id/page_2"
android:enabled="true"
android:icon="@drawable/two"
android:title="label_2"/>
</menu>
bottomNavigationView.setOnNavigationItemSelectedListener
setOnNavigationItemReselectedListener
reselect应该是在select之前的事件,因为没有被拦截
各种属性
app:backgroundTint="@color/colorPink"//背景颜色
app:menu="@menu/bottom_navigation_menu"//列表
app:itemRippleColor="@color/colorGhostWhith"//水波纹
app:labelVisibilityMode="labeled"//标签是否需要点击才能显示 全部显示尽量加上选中效果
app:itemIconTint="@color/colorYellow"//ICON构筑颜色
app:itemTextColor="@color/colorLightGreen"//ICON字颜色
app:itemTextAppearanceActive="@style/Active"//选中状态
app:itemTextAppearanceInactive="@style/Inactive"//未选中状态
Button
//文字主题按钮
<Button
......
android:text="Text button"
app:strokeColor = "@color/colorBoderBlue"//边界颜色
app:strokeWidth = "1dp"//边界宽
app:rippleColor = "@color/colorPink"//水波纹
style="@style/Widget.MaterialComponents.Button.TextButton"
/>
//ICON主题按钮
<Button
......
app:backgroundTint="@color/colorPink"//颜色
app:icon="@mipmap/onei"
app:iconGravity = "end"//icon相对位置
app:iconPadding="5dp"//icon与文字距离
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
/>
<Button
...//单纯的有外框按钮 不需要自己写
android:text="Outlined button"
style="?attr/materialButtonOutlinedStyle"/>
<Button
...//外框加上icon
android:text="Outlined button"
app:icon="@mipmap/onei"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"/>
文字按钮组
MaterialButtonToggleGroup materialButtonToggleGroup = findViewById(R.id.toggleButton) ;
materialButtonToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
@Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
switch (checkedId)
{
case R.id.button1:
if (isChecked)
{
Toast.makeText(MainActivity.this,"button1 is check",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"button1 is not check",Toast.LENGTH_SHORT).show();
}
break;
case...
}
}
});
icon按钮组
<com.google.android.material.button.MaterialButtonToggleGroup
...>
<Button
...
app:icon="@drawable/ic_favorite_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
<Button
...
app:icon="@drawable/ic_remove_red_eye_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
<Button
...
app:icon="@drawable/ic_notifications_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Style
<style name="Widget.App.Button.OutlinedButton.IconOnly" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="iconPadding">0dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingRight">12dp</item>
<item name="android:minWidth">48dp</item>
<item name="android:minHeight">48dp</item>
</style>
使用的大控件一样,监听方法也一样
group里面使用方法
app:singleSelection = "true"
app:selectionRequired = “true"
可以决定是否单选 是否必选
复选框
<CheckBox
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/sl_favourite_24dp"
app:buttonTint="@android:color/white"
/>
In res/drawable/sl_favourite_24dp.xml:
<selector>
<item
android:drawable="@drawable/ic_favourite_outlined_24dp"
android:state_checked="false"
/>
<item
android:drawable="@drawable/ic_favourite_filled_24dp"
android:state_checked="true"
/>
<item android:drawable="@drawable/ic_favourite_outlined_24dp" />
</selector>
这样的checkbox可以和平时一样正常使用
ExtendedFloatingActionButton
后两个方法仅FloatingActionButton
// To show:
fab.show()
// To hide:
fab.hide()
// To extend:
fab.extend()
// To shrink:
fab.shrink()
配合CoordinatorLayout使用
<androidx.coordinatorlayout.widget.CoordinatorLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:contentDescription="@string/fab_content_desc"
app:srcCompat="@drawable/ic_plus_24"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
app:fabSize="mini(auto normal)"决定按钮大小
app:backgroundTint 背景颜色
app:borderWidth 边界宽