如何将 BottomAppBar 的背景设置为位图?
How to set a BottomAppBar's background as a bitmap?
下面我有一张位图,不是纯白的,有一些噪点:
如何设置BottomAppBar的背景,bitmap.xml如下,
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg"
android:tileModeX="repeat"
android:tileModeY="repeat" />
在我的 activity_main.xml 中,我使用它如下:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bitmap"
app:fabAlignmentMode="center"
app:fabAnimationMode="scale"
app:navigationIcon="@drawable/ic_settings" />
但是不行,BottomAppBar的背景没有显示位图,还是纯白色,我在其他布局中使用bitmap.xml成功,但是BottomAppBar不行。
我将 xml 的 android:background="@drawable/bitmap" 更改为 java 代码:
bottomAppBar.setBackground(getResources().getDrawable(R.drawable.bitmap));
但也有问题,往下看
中心圈应该像下面这样,但是现在周围都是背景位图:
看来这不可能。 According to this:
The BottomAppBar internally handles its own background. This allows it to automatically cradle the FloatingActionButton when it is attached, but it also means that you shouldn’t call setBackground() or use the android:background attribute in xml. Instead, the app:backgroundTint attribute will allow you to set a tint.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity" >
<!-- Content -->
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabAlignmentMode="center"
app:navigationIcon="@drawable/ic_menu"
app:menu="@menu/actions"
android:animateLayoutChanges="true" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:backgroundTint="@color/colorAccent"
app:tint="@android:color/white"
app:srcCompat="@drawable/ic_record"
app:layout_anchor="@id/bar" />
<FrameLayout
android:id="@+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
app:behavior_hideable="true"
app:layout_behavior="@string/bottom_sheet_behavior">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_sheet"
app:itemIconTint="?attr/bottom_bar_ic_tint"
app:elevation="0dp"
app:menu="@menu/primary"/>
</FrameLayout>
下面我有一张位图,不是纯白的,有一些噪点:
如何设置BottomAppBar的背景,bitmap.xml如下,
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg"
android:tileModeX="repeat"
android:tileModeY="repeat" />
在我的 activity_main.xml 中,我使用它如下:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bitmap"
app:fabAlignmentMode="center"
app:fabAnimationMode="scale"
app:navigationIcon="@drawable/ic_settings" />
但是不行,BottomAppBar的背景没有显示位图,还是纯白色,我在其他布局中使用bitmap.xml成功,但是BottomAppBar不行。
我将 xml 的 android:background="@drawable/bitmap" 更改为 java 代码:
bottomAppBar.setBackground(getResources().getDrawable(R.drawable.bitmap));
但也有问题,往下看
中心圈应该像下面这样,但是现在周围都是背景位图:
看来这不可能。 According to this:
The BottomAppBar internally handles its own background. This allows it to automatically cradle the FloatingActionButton when it is attached, but it also means that you shouldn’t call setBackground() or use the android:background attribute in xml. Instead, the app:backgroundTint attribute will allow you to set a tint.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity" >
<!-- Content -->
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabAlignmentMode="center"
app:navigationIcon="@drawable/ic_menu"
app:menu="@menu/actions"
android:animateLayoutChanges="true" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:backgroundTint="@color/colorAccent"
app:tint="@android:color/white"
app:srcCompat="@drawable/ic_record"
app:layout_anchor="@id/bar" />
<FrameLayout
android:id="@+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
app:behavior_hideable="true"
app:layout_behavior="@string/bottom_sheet_behavior">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_sheet"
app:itemIconTint="?attr/bottom_bar_ic_tint"
app:elevation="0dp"
app:menu="@menu/primary"/>
</FrameLayout>