【问题标题】:Android PopupWindow elevation does not show shadowAndroid PopupWindow 高程不显示阴影
【发布时间】:2016-08-06 06:24:23
【问题描述】:

Android PopupWindow 在设置高度时不显示阴影。它似乎从文档中支持它。我正在使用 5.0 棒棒糖。

如下创建弹出窗口:

    popupWindow = new PopupWindow(context);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
    popupWindow.setElevation(10);
    popupWindow.setContentView(rootView);
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, xPos, yPos);

【问题讨论】:

  • 您是否在应用程序的其他地方出现阴影,例如来自 ActionBar?
  • 是的,阴影在其他任何地方都可以正常工作。
  • Patrick,你能解决这个问题吗?
  • 我不得不推出自己的解决方案。

标签: android android-5.0-lollipop android-popupwindow


【解决方案1】:

作为answered by an Android developer

如果膨胀视图没有设置背景,或者弹出 窗口本身没有设置背景(或有一个透明的 背景),那么您将不会得到阴影。

这是我的情况,似乎是你的,因为你没有使用 setBackgroundDrawable。

这对我有用

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

我打开了一个新问题,建议他们更新文档 (https://code.google.com/p/android/issues/detail?id=174919)

【讨论】:

  • 如果我想使用背景而不是颜色怎么办?
  • @Dr.aNdRO 在你的drawable中实现投影
  • 我就是这么做的,谢谢!
  • 搜索了几个小时的原因,为什么它不起作用。谢谢
  • 如果我想使用自定义背景可绘制对象怎么办?像泡沫一样?我没有看到任何阴影
【解决方案2】:

对于访问此答案并错过 OP 已有内容的其他人,您应该设置高程以创建阴影:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    popupWindow.setElevation(20);
}

根据您的内容视图,您可能还需要设置背景可绘制对象,尽管这并不总是必要的。如果需要,您可以按照@Maragues 的建议进行操作:

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

要支持棒棒糖之前的设备,您可以使用 9-patch 或包含阴影的图像。

代码

这是上图的代码。

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        popupWindow.dismiss();
        return true;
    }
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    popupWindow.setElevation(20);
}

popupWindow.showAtLocation(anyView, Gravity.CENTER, 0, 0);

注意:

在代码中设置时高程以像素为单位,但在 xml 中设置时通常以 dp 为单位。在代码中设置时应将 dp 值转换为像素。

【讨论】:

    【解决方案3】:
    • setElevation 没有显示阴影,因为我的容器是透明的
    • 我的容器是透明的,因为我需要在每一侧进行一些填充

    Screenshot of the below code

    • 我做了三个容器
    • 最外面的容器是透明的
    • 里面的下一个容器有一个带有阴影的可绘制背景
    • 下一个容器保存实际内容
    • xml 内按钮的最小宽度有助于确定宽度。与第二个容器的 12dp 内边距相同。

    用 Kotlin 编写的自定义弹出窗口类:

    class CustomPopupWindow(
        private val context: Context
    ) : PopupWindow(context) {
    
      init {
        val view = LayoutInflater.from(context).inflate(R.layout.popup_window_layout, null)
        contentView = view
    
        height = ListPopupWindow.WRAP_CONTENT
        width = ListPopupWindow.MATCH_PARENT
        isOutsideTouchable = true
    
        setTouchDismissListener()
    
        // set the background of the second container to the drawable
        // with the shadow to get our shadow
        contentView.findViewById<LinearLayout>(R.id.outer_content_container).setBackgroundDrawable(context.resources.getDrawable(R.drawable.background_shadow))
      }
    
      // Add a listener to dismiss the popup Window when someone
      // clicks outside of it
      private fun setTouchDismissListener() {
        setTouchInterceptor { _, event ->
          if (event != null && event.action == MotionEvent.ACTION_OUTSIDE) {
            dismiss()
            return@setTouchInterceptor true
          }
          false
        }
      }
    
      // this anchor view can be ANY view
      fun show(anchor: View) {
    
        // Remove the default background that is annoying
        setBackgroundDrawable(BitmapDrawable())
    
        // Grab the pixel count for how far down you want to put it.
        // toolbar_height is 56dp for me
        val yOffSetInPixels = context.resources.getDimensionPixelSize(R.dimen.toolbar_height)
    
        // Animation to make it appear and disappear like a Dialog
        animationStyle = android.R.style.Animation_Dialog
    
        // Show it
        showAtLocation(anchor, Gravity.TOP, 0, yOffSetInPixels)
      }
    }
    
    
    • 自定义 PopupWindow 的 XML:
    <?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"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:orientation="vertical">
    
      <android.support.constraint.ConstraintLayout
        android:id="@+id/transparent_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:padding="12dp">
    
        <LinearLayout
          android:id="@+id/outer_content_container"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/white"
          android:orientation="vertical"
          app:layout_constraintBottom_toBottomOf="@+id/transparent_container"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/transparent_container">
    
          <LinearLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="12dp">
    
            <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Header" />
    
            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_gravity="center_vertical"
              android:layout_marginTop="8dp"
              android:orientation="horizontal">
    
              <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingEnd="0dp"
                android:paddingStart="8dp"
                android:text="Message" />
    
            </LinearLayout>
    
            <TextView
              android:id="@+id/add_to_bag_button"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="16dp"
              android:height="48dp"
              android:background="@color/gray"
              android:gravity="center"
              android:minWidth="350dp"
              android:text="BUTTON"
              android:textAllCaps="true" />
    
          </LinearLayout>
    
        </LinearLayout>
    
      </android.support.constraint.ConstraintLayout>
    
    </LinearLayout>
    
    
    • 显示阴影的自定义 Drawable:
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
      <!-- Drop Shadow Stack -->
      <item>
        <shape>
          <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="0dp" />
    
          <solid android:color="#00CCCCCC" />
    
          <corners android:radius="3dp" />
        </shape>
      </item>
      <item>
        <shape>
          <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="0dp" />
    
          <solid android:color="#10CCCCCC" />
    
          <corners android:radius="3dp" />
        </shape>
      </item>
      <item>
        <shape>
          <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="0dp" />
    
          <solid android:color="#20CCCCCC" />
    
          <corners android:radius="3dp" />
        </shape>
      </item>
      <item>
        <shape>
          <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="0dp" />
    
          <solid android:color="#30CCCCCC" />
    
          <corners android:radius="3dp" />
        </shape>
      </item>
      <item>
        <shape>
          <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="0dp" />
    
          <solid android:color="#50CCCCCC" />
    
          <corners android:radius="3dp" />
        </shape>
      </item>
    
      <!-- Background -->
      <item>
        <shape>
          <solid android:color="@android:color/white" />
    
          <corners android:radius="0dp" />
        </shape>
      </item>
    
    </layer-list>
    
    
    • 全部使用:
    val popupWindow = CustomPopupWindow(activity);
    popupWindow.show(anyViewInYourActivity);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-10
      • 2017-09-06
      • 2015-08-22
      • 2015-02-13
      • 2021-05-21
      • 2018-01-03
      相关资源
      最近更新 更多