首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓PopupWindow elevation未显示阴影

安卓PopupWindow elevation未显示阴影
EN

Stack Overflow用户
提问于 2014-12-03 05:47:45
回答 3查看 23.1K关注 0票数 36

设置了标高后,Android PopupWindow不会显示阴影。从文档中看,它似乎支持它。我使用的是5.0版的棒棒糖。

如下所示创建弹出窗口:

代码语言:javascript
复制
    popupWindow = new PopupWindow(context);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
    popupWindow.setElevation(10);
    popupWindow.setContentView(rootView);
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, xPos, yPos);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-28 16:22:01

作为answered by an Android developer

如果放大的视图没有背景设置,或者弹出窗口本身没有背景设置(或者有透明的背景),那么你就得不到阴影。

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

这对我很有效

代码语言:javascript
复制
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

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

票数 34
EN

Stack Overflow用户

发布于 2018-05-07 17:57:54

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

代码语言:javascript
复制
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    popupWindow.setElevation(20);
}

根据您的内容视图,您可能还需要将背景设置为可绘制,尽管这并不总是必要的。如果需要,你可以按照@Maragues建议的那样做:

代码语言:javascript
复制
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

为了支持之前的棒棒糖设备,你可以使用包含阴影的9补丁或图像。

代码

这是上图的代码。

代码语言:javascript
复制
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值时,应将其转换为像素。

票数 15
EN

Stack Overflow用户

发布于 2019-02-07 03:02:07

  • setElevation没有显示阴影,因为我的容器是透明的,我的容器是透明的,因为我需要在每一面上都有一些填充

Screenshot of the below code

Next容器我做了三个containers

  • Outer,最大的容器是transparent

  • Next容器里面有一个可绘制的背景与阴影

  • 下一个容器持有实际的内容

  • 最小宽度的按钮里面的xml帮助规定宽度。与第二个容器的12dp填充相同。

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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

显示阴影的

  • 自定义绘图:

代码语言:javascript
复制
<?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>

使用它的

代码语言:javascript
复制
val popupWindow = CustomPopupWindow(activity);
popupWindow.show(anyViewInYourActivity);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27259614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档