草庐IT

android - 是否可以从另一个 PopupWindow 中显示一个 PopupWindow?

coder 2023-12-24 原文

在 Honeycomb 应用程序中,我在多个地方使用 PopupWindow 的自定义子类来显示各种 View 。这一切都很好,直到其中一个 View 碰巧尝试显示另一个 PopupWindow。

例如,Spinner 和 AutoCompleteTextView 都使用 PopupWindow 来显示其关联的选择列表。如果您将其中一个放在 PopupWindow 的 View 中,并单击以激活该小部件,则 WindowManager 将通过 LogCat 警告您:

WARN/WindowManager(111):尝试添加带有子窗口 token 的窗口:android.os.BinderProxy@40ea6880。中止。

然后它会在实际尝试显示 PopupWindow 时抛出 WindowManager$BadTokenException

自定义 PopupWindow 的 View 正在使用从 anchor View 的 Context 获得的 LayoutInflater 进行扩充。我看到其他问题表明在使用不适当的 Context 获取 LayoutInflater 时可能会发生 BadTokenExceptions,但在这种情况下似乎没有其他选择。

来自 WindowManager 的日志警告似乎表明这是一个不受支持的案例。谁能证实这一点,或者提供一根棍子让我朝正确的方向戳?

这里有一个指向错误案例起源的代码(无论如何,它的某个版本)的链接:WindowManagerService.java

最佳答案

确定您可以从另一个 PopupWindow 中显示一个 PopupWindow,只需创建一个新的 pop_up 窗口变量并inflate 新的 View 中的 PopupWindow 布局,就像下面的示例:

public PopupWindow pw;
public PopupWindow new_pw;
public View pw_layout = null;
public View new_pw_layout = null;

public void initiatePopupWindow() throws Exception{
// to get the instance of the LayoutInflater
        LayoutInflater inflater = (LayoutInflater) YourCurrentActivityClass.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

pw_layout = inflater.inflate(R.layout.first_popup_xml,
                    (ViewGroup)findViewById(R.id.first_popup_main_layout));

// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(pw_layout,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, true);
// display the popup in the center
pw.showAtLocation(pw_layout, Gravity.CENTER, 0, 0);

Button item_button = (Button) pw_layout.findViewById(R.id.item);
                item_button .setOnClickListener(onItemlClick);

}


// the onClick listener for the item button
public OnClickListener onItemlClick = new OnClickListener() {
        public void onClick(View v) {

if(v == item_button){
LayoutInflater inflater = (LayoutInflater) YourCurrentActivityClass.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
new_pw_layout = inflater.inflate(R.layout.second_popup_xml,
                    (ViewGroup)findViewById(R.id.second_popup_main_layout));
// create a 300px width and 470px height new PopupWindow
new_pw = new PopupWindow(new_pw_layout,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);
// display the new popup in the center
new_pw.showAtLocation(new_pw_layout, Gravity.CENTER, 0, 0);// you can set it's position here([0,0] means in the exact center it's like [go 0 pixels from the center to the right , go 0 pixels from the center down])

}
}
};

关于android - 是否可以从另一个 PopupWindow 中显示一个 PopupWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296538/

有关android - 是否可以从另一个 PopupWindow 中显示一个 PopupWindow?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  8. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  9. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  10. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

随机推荐