【问题标题】:How to set a BottomAppBar's background as a bitmap?如何将 BottomAppBar 的背景设置为位图?
【发布时间】:2019-01-10 08:07:05
【问题描述】:

我下面有一张位图,不是纯白色的,有一些噪点

如何设置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));

但也有问题,往下看

中心圆应该如下所示,但现在周围都是背景位图:

【问题讨论】:

    标签: android material-design android-bottomappbar


    【解决方案1】:

    看起来这是不可能的。 According to this:

    BottomAppBar 在内部处理自己的背景。这允许它在附加 FloatingActionButton 时自动放置它,但这也意味着您不应该调用 setBackground() 或在 xml 中使用 android:background 属性。相反,app:backgroundTint 属性将允许您设置色调。

    【讨论】:

    • 谢谢!现在看来只能换颜色了。
    【解决方案2】:
    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 2013-10-30
      相关资源
      最近更新 更多