【问题标题】:How can I get a reference of a view from a inflated layout如何从膨胀的布局中获取视图的参考
【发布时间】:2023-03-26 22:51:01
【问题描述】:

我有一个巨大的活动布局,我使用setContentView() 设置。在那个布局中,我有一个我想要填充的TableLayout(在我的活动中命名为tableLayout)。该 TableLayout 的行是布局文件中的自定义视图(我们称该文件为tablerow_layout.xml)。在这个布局中,我有一些 TextViews 和 ImageView。我想要做的是在创建行时以编程方式更改 TextView 的内容。这是我的代码:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow. findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);

不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。我知道我可以使用 ListView 来做到这一点,但我通常只有少量的行,并且为 ListView 创建另一个适配器的开销非常大。

正如@ρяσѕρєя K 建议的那样,我也尝试了 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别

【问题讨论】:

    标签: android android-tablelayout layout-inflater


    【解决方案1】:

    您没有在 Textview 中设置 newRow

      View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
      TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
    

    【讨论】:

      【解决方案2】:

      如何从膨胀的布局中获取视图的引用

      使用从LayoutInflater.inflate 方法返回的View 对象从布局访问视图,该布局作为第一个参数传递给inflate 方法。

      在您的情况下,使用newRow 对象从R.layout.tablerow_layout 布局访问视图:

      TextView name = (TextView)newRow.findViewById(R.id.tablerow_name); 
      

      【讨论】:

      • 之前尝试过,但没有任何区别。准备编辑我的问题
      【解决方案3】:
        try
      
         {
         LayoutInflater layoutInflater = (LayoutInflater)   
         getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      
           View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null,   false);
           TextView name = (TextView) newRow.findViewById(R.id.tablerow_name);
      
           name.setText("THIS LINE");
      
          tableLayout.addView(newRow);
      
          }catch(Exception e)
      
          {
      
          e.printTracktrace();
      
          }
      

      【讨论】:

      • 它只是阻止我的应用程序崩溃,而不设置实际文本。
      【解决方案4】:

      使用 IntelliJ Amiya 和 ρяσѕρєя K 的答案,这个代码对我有用。

      LayoutInflater inflater = LayoutInflater.from(this);
          LinearLayout linearLayout = 
      (LinearLayout)findViewById(R.id.profileView);
          ViewGroup viewGroup = linearLayout;
      
          for (int i = 0; i < NUM_OF_PROFILES; i++){
      
              LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.profile_badge_layout, viewGroup, false);
              linearLayout.addView(layout);
      
              TextView names = (TextView)layout.findViewById(R.id.profileName);
              ImageView images = (ImageView)layout.findViewById(R.id.profileImage);
      
              names.setText(""+i);
      
      
              viewGroup = layout;
          }
      

      每个文本都有它自己的 i 值。我相信您需要先添加视图。然后你可以改变值

      【讨论】:

        猜你喜欢
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多