当我使用弹出窗口显示时,我希望能够模糊或调暗背景popup.showAtLocation,并在popup.dismiss调用时将背景解开/调暗.
我曾尝试应用布局PARAMS FLAG_BLUR_BEHIND和FLAG_DIM_BEHIND我的活动,但这似乎只是一旦我的应用程序开始模糊和朦胧的背景.
如何使用弹出框进行模糊/调光?
我在线查看,无法找到PopupWindow类的工作示例.我在网上找到的代码示例要么编译但是不起作用,要么使用已被删除的方法(例如Activity.getViewInflate()).
是否有一个显示PopupWindow 的简单工作示例?
要创建一个简单的工作PopupWindow,我们需要执行以下操作:
popup_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java代码
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
Run Code Online (Sandbox Code Playgroud)
我的要求是我需要一个
<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
和a
<BUTTON android:id="@+id/end_data_send_button" android:text="Cancel"/>
Run Code Online (Sandbox Code Playgroud)
在我的popup_example.xml.如何在Java代码中处理这两个组件?

我在我的应用程序中有一个弹出窗口,当点击某个按钮时它出现我想将动画淡入到此窗口,我将xml文件放在"res/anim"文件夹中并为弹出窗口设置动画样式但是动画不起作用?这是我的代码:
myanim.xml ...
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="4000"
android:repeatCount="1"/>
</set>
Run Code Online (Sandbox Code Playgroud)
===============================================
创建弹出窗口
private PopupWindow showOptions(Context mcon){
try{
LayoutInflater inflater = (LayoutInflater) mcon.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.options_layout,null);
layout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.myanim));
PopupWindow optionspu = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
optionspu.setFocusable(true);
optionspu.showAtLocation(layout, Gravity.TOP, 0, 0);
optionspu.update(0, 0, LayoutParams.WRAP_CONTENT, (int)(hei/5));
optionspu.setAnimationStyle(R.anim.myanim);
return optionspu;
}
catch (Exception e){e.printStackTrace();
return null;}
}
Run Code Online (Sandbox Code Playgroud)
================================================= onClick method ...(optionsPopup是PopupWindow类型的全局变量)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.options:
optionsPopup=showOptions(this);
break;
}
Run Code Online (Sandbox Code Playgroud) 设置标高时,Android Popwindow不显示阴影.它似乎从文档中支持它.我正在使用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);
Run Code Online (Sandbox Code Playgroud) 我不断收到这个错误,当我试图setErrorEnabled在我的textInputLayout:
03-12 12:29:03.206 5706-5706/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 5706
java.lang.RuntimeException: Failed to resolve attribute at index 24
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:696)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at android.widget.TextView.<init>(TextView.java:624)
at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
at com.bekwaai.popupwindow.RGNamePopUp$1.onClick(RGNamePopUp.java:48)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Run Code Online (Sandbox Code Playgroud)
这是场景 - 我有一个textInputLayout内部popupwindow.这是popupwidow中的代码.
public class NamePopUp extends PopupWindow {
android.support.v7.widget.AppCompatEditText name;
TextInputLayout nameInput;
public NamePopUp(final Context context) …Run Code Online (Sandbox Code Playgroud) android android-edittext android-popupwindow android-textinputlayout
我正在使用3个Spinners和一个EditText 做一个Popup.在阅读了很多博客和文章后,我无法确定哪个是最佳选择,PopupWindow或DialogFragment.
标准是:
如果有更好的方法,我愿意改变观点.
非常感谢你.
DialogFragment:
弹出窗口:
编辑:结论
DialogFragment允许您使用更复杂的功能.另一个重要的事情是它更加平板电脑,因为它允许用户一次打开多个片段.片段是一种更动态的解决方案,它们是较新Android版本的标准.
PopupWindow更简单,对用户的简单问题非常有帮助.
我不得不创建一个类似微调器的视图,它具有以下行为:
我已经成功制作了这个视图(下面的代码),但出于某种原因,在Android 7.1上,我让弹出菜单出现在视图的顶部(甚至重叠它和它上面的视图),而不是它下面,因为它应该.这是它的样子,它应该是什么样子:
由于它是相当多的代码和资源,我把它全部放在一个Github存储库(这里),但这里是代码的主要部分(我在修复它的尝试是在注释中):
FullSizePopupSpinner.java
public class FullSizePopupSpinner extends android.support.v7.widget.AppCompatTextView {
private static final long ANIMATION_DURATION = 150;
private int[] mItemsTextsResIds, mItemsIconsResIds;
private int mSelectedItemPosition = -1;
private SpinnerPopupWindow mPopupWindow;
private boolean mInitialized = false;
private OnItemSelectedListener mOnItemSelectedListener;
private Drawable mClosedDrawable;
private Drawable mOpenedDrawable;
public interface OnItemSelectedListener {
void onItemSelected(FullSizePopupSpinner parent, int position, String item, int previousSelectedPosition);
void onNothingSelected(FullSizePopupSpinner parent);
}
public FullSizePopupSpinner(final Context context) {
super(context);
init(context);
}
public FullSizePopupSpinner(final Context …Run Code Online (Sandbox Code Playgroud) 我使用以下代码来显示一个小弹出窗口:
public static PopupWindow showImportMenu(Activity activity, View anchor, PopupWindowClickListener onClickListener)
{
LayoutInflater inflater = LayoutInflater.from(activity);
PopupImportBinding binding = DataBindingUtil.inflate(inflater, R.layout.popup_import, null, false);
if (!RootTools.isRootAvailable())
binding.llImportRootMethod.setVisibility(View.GONE);
PopupWindow popupWindow = new PopupWindow(activity, null, R.attr.popupMenuStyle);
popupWindow.setFocusable(true);
popupWindow.setContentView(binding.getRoot());
popupWindow.setOutsideTouchable(true);
PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.BOTTOM);
View.OnClickListener clickListener = new View.OnClickListener()
{
@Override
public void onClick(View view)
{
onClickListener.onClick(popupWindow, view);
}
};
binding.llImportDefault.setOnClickListener(clickListener);
binding.llImportRootMethod.setOnClickListener(clickListener);
binding.llImportHTCFromContacts.setOnClickListener(clickListener);
binding.llImportManual.setOnClickListener(clickListener);
return popupWindow;
}
Run Code Online (Sandbox Code Playgroud)
这适用于很多设备,但在一些罕见的设备上它不起作用,如:
我收到的反馈是没有显示弹出窗口.有谁知道为什么这不适用于上述设备?我能做些什么才能让它在这个设备上运行?
编辑
似乎我不想清楚我想要的是:
showAsDropDown没有showAtLocation或类似的,我从来没有看到这个问题showAtLocation还没有 …