草庐IT

android - 无法将膨胀 View 添加到自定义 View 组

coder 2024-07-03 原文

我一直在尝试将 View 从 xml 扩展到它们相应的 View 对象,然后将这些 View 对象添加到编程的 View 组中。但是,每当我调用 myviewgroup.addView(childview) 时,childview 似乎丢失了所有按钮和 xml 代码应该给它的东西。但是, subview 确实具有的一件事是背景颜色,我将其编程以确保为 subview 提供了正确的尺寸。 subview 被赋予了正确的尺寸,但其组件均不可见

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    viewchanger= new ViewChanger(this); //my viewgroup
    View mainmenu= inflater.inflate(R.layout.main, null); //the xml code
    mainmenu.setBackgroundColor(Color.rgb(0, 0, 255));
    viewchanger.addView(mainmenu);
    setContentView(viewchanger); 

上面的代码只是将我的屏幕背景设置为蓝色,但没有显示任何按钮。此外,当我 setContentView(mainmenu) 时,该 xml 代码中的所有内容以及背景颜色都完美显示。这个 View 组的目的是让我可以在多个 View 中的任何一个之间切换,而不管它们作为 subview 的顺序如何。

这里是viewchanger的代码。我希望它有 7 个 subview ,当我调用一个方法时 setView(int v) 我希望所有其他 View 都消失,我只希望一个 View 可见。

public class ViewChanger extends ViewGroup{

/**Represent which view to show, only one can show at a time*/
public static final int MainMenu=0, ChooseMap=1, MapOptions=2, 
        SetLocation=3, view=4, EditMap=5, CreateMap=6;
private int current; //the current view
private View[] views= new View[7];

public ViewChanger(Context context) {
    super(context);
            //load 7 views into view array here
            //LayoutInflater inflater =    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           /*views[0]= inflater.inflate(R.layout.main, null);
            * views[1]= inflater.inflate(R.layout.choose, null);
            * views[2]= inflater.inflate(R.layout.mapoptions, null);
        * views[0].setBackgroundColor(Color.rgb(255, 0, 0));
    * views[1].setBackgroundColor(Color.rgb(255, 255, 0));
    * views[2].setBackgroundColor(Color.rgb(0, 255, 0));*/
    * addView(views[0]);
    * addView(views[1]);
    * addView(views[2]);
            * this is how i want to load the childviews*/

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    for(int i = 0; i < getChildCount(); i++){
                getChildAt(i).layout(0, 0, getWidth(), getHeight());
            }
}
    /**Sets the view of the view group, only one view can be viewed at a time*/
public void setView(int v){
            current=v;
    for (int i=0; i<views.length; i++){
        if (v==i){
            views[i].setVisibility(View.VISIBLE);
        } else{
            views[i].setVisibility(View.GONE);
        }
    }
}

当我在 ViewChanger 构造函数中加载 subview 时, View 组只显示我通过 setView(int v) 设置的任何 View 的背景颜色,而不显示按钮。

这是 main 的 xml 代码,但所有其他代码看起来都与此非常相似:

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:gravity="center"
   android:orientation="vertical" >


 <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="The title"
    android:textAppearance="?android:attr/textAppearanceLarge" />


 <TextView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome [...]"
    android:textAppearance="?android:attr/textAppearanceLarge" />




 <Button
    android:id="@+id/load"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Load Map" />




 <Button
    android:id="@+id/create"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Create Map" />






 <Button
    android:id="@+id/users"
    android:layout_width="122dp"
    android:layout_height="wrap_content"
    android:text="Other User" />

</LinearLayout>

最佳答案

在思考完我能想到的所有内容后,我创建了一个与此相同的虚拟项目来检查几种可能性。出于某种原因,让 ViewChanger 从 FrameLayout 而不是 ViewGroup 扩展使得这项工作 - 很可能是因为 ViewGroup 缺少渲染它的 subview 所需的东西(我尝试覆盖 onDraw,但它仍然没有工作)。由于您一次只显示一个 View - 通常使用 FrameLayout - 我建议只进行此更改;这可能比隔离和修复 ViewGroup 中缺失的绘制功能要容易得多。

public class ViewChanger extends FrameLayout {

应该是使原始设计(从构造函数添加 View )正常工作所需进行的唯一更改。

关于android - 无法将膨胀 View 添加到自定义 View 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055735/

有关android - 无法将膨胀 View 添加到自定义 View 组的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

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

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

  5. 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=>

  6. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  7. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

随机推荐