首页 > 解决方案 > 在 Android 的底页顶部添加图像

问题描述

如何在 Android 的底部工作表顶部添加图像?看到这张图片

我试过这个,但它不起作用。我想要如图所示的透明度。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/myBottomLL"
        app:layout_anchorGravity="top|center" />

    <LinearLayout
        android:id="@+id/myBottomLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/background" />

    </LinearLayout>

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

标签: android

解决方案


按照这些链接。如何使背景透明: https ://medium.com/@manuaravindpta/transparent-background-for-bottomsheetdialog-396a1a646f1b

如何使底页全屏: https ://medium.com/better-programming/bottom-sheet-android-340703e114d2

对于圆形图像视图: https ://github.com/hdodenhof/CircleImageView

只需像这样在顶部添加一个透明视图。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/topImage"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_gravity="center_horizontal|top"
        android:elevation="6dp"
        android:src="@drawable/ic_account_circle"
        app:civ_border_color="@color/colorAccent"
        app:civ_border_width="2dp"
        app:civ_circle_background_color="@android:color/white" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/transparentRegion"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal" />


        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/white"
            app:elevation="0dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/closeBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="16dp"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/ic_close"
                    android:tint="@android:color/black" />
            </LinearLayout>


        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/bottomSheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:fillViewport="true">

            <LinearLayout
                android:id="@+id/other_content_goes_here"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="15dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Other Content"
                    android:textSize="24sp" />

                <View
                    android:id="@+id/extraSpace"
                    android:layout_width="match_parent"
                    android:layout_height="800dp" />

            </LinearLayout>


        </androidx.core.widget.NestedScrollView>


    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

设置 BottomSheetDialogFragment 的主题。您还可以添加一个 BottomSheetCallback 并在向上滚动时减小顶部透明空间的高度


推荐阅读