Android PopupWindow高程不显示阴影

时间:2014-12-02 21:47:45

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

设置标高时,Android PopupWindow不会显示阴影。它似乎从文档中支持它。我正在使用5.0 Lollipop。

按如下方式创建弹出窗口:

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

3 个答案:

答案 0 :(得分:21)

作为answered by an Android developer

  

如果膨胀的视图没有背景设置或弹出窗口   窗口本身没有背景设置(或具有透明度   然后你不会得到影子。

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

这对我有用

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

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

答案 1 :(得分:4)

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

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

PopupWindow with shadow

根据您的内容视图的不同,您可能还需要设置背景可绘制,但这并非总是必要的。如果需要,你可以像@Maragues建议的那样做:

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

要支持前Lollipop设备,您可以使用包含阴影的9补丁或图像。

代码

这是上图的代码。

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值转换为像素。

答案 2 :(得分:0)

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

Screenshot of the below code

  • 我做了三个容器
  • 最外面的容器是透明的
  • 里面的下一个容器有一个带有阴影的Drawable背景
  • 下一个容器保存实际内容
  • 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);