【问题标题】:Add multiple linearLayouts inside another LinearLayout在另一个 LinearLayout 中添加多个 linearLayout
【发布时间】:2012-07-24 11:23:35
【问题描述】:

我的 Android 应用程序出现问题。我需要在另一个线性布局中添加线性布局。我正在使用通货膨胀。我试图解释得更好。

 __________________________________________________
|                                                  |
|                                  BUTTON PLUS 1   |
|   __________________________________________     |
|   |                layout1   Button Plus 2  |    |
|   |    ______________btnRemove1________     |    |
|   |    |                       layout2 |    |    |
|   |    |                               |    |    |
|   |    |_____________btnRemove2________|    |    |
|   |_________________________________________|    |   
|                                                  |
|   __________________________________________     |
|   |             layout1  Button Plus 2      |    |
|   |    ______________btnRemove1________     |    |
|   |    |                       layout2 |    |    |
|   |    |                               |    |    |
|   |    |_____________btnRemove2________|    |    |
|   |_________________________________________|    |
|                                                  |
|__________________________________________________|

我的目标是:当我点击按钮 plus1 时,添加 layout1。当我单击按钮 plus2 时,在 layout1 中添加布局 2。我的问题是我需要这个多次和具有不同 id 的布局。

当我点击 btnRemove1 时,我需要同时删除 layout1 和 layout2。当我单击 btnRemove2 时,我需要删除 layout2。

这是我的代码

 //Button plus 1    
      case R.id.se_ibAddAddressPostal:
        LayoutInflater inflaterAddressPostal = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout addressPostal = (LinearLayout)inflaterAddressPostal.inflate(R.layout.socio_form_structured_postal, null);
        LinearLayout lLayoutAddressPostal;
        lLayoutAddressPostal = (LinearLayout)findViewById(R.id.se_contentAdressPostal);
        lLayoutAddressPostal.addView(addressPostal);

        break;

    //Button plus 2
      case R.id.sfsp_ivMoreAddressPostal:
        ImageButton imbtMoreAddress = (ImageButton)findViewById(R.id.sfsp_ivMoreAddressPostal);
        imbtMoreAddress.setVisibility(imbtMoreAddress.INVISIBLE);

        LayoutInflater inflaterAddressPostal2 = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout addressPostal2 = (LinearLayout)inflaterAddressPostal2.inflate(R.layout.socio_form_structured_postal2,null);
        LinearLayout lLayoutAddressPostal2;
        lLayoutAddressPostal2 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);
        lLayoutAddressPostal2.addView(addressPostal2);
        break;

【问题讨论】:

  • 嘿里卡多!欢迎来到 SO。您想出了一个不错的应用程序创意。但是您必须采取措施并努力实施它。与我们分享一些您编写的代码或您在什么时候未能实现它。每个人都在这里为您提供帮助。
  • 我猜你正在尝试重新实现 ExpandableListView。这是一个完全可自定义的元素,每次您告诉它时都会显示子列表。
  • 我更新了我的问题。我需要的是添加一个layout1。当我单击按钮 plus2 时,我需要在 layout1 中插入一个 layout1。我需要这个多次和不同的 id 布局
  • 您可以通过 布局并根据需要设置可见性来实现此目的。
  • 我的问题是,当我在第一个 layout1 中单击按钮加 2 时,他在第二个 layout1 下方添加了 layout2。我的问题在于 id,因为我每次都添加相同的内容,但我不知道如何修复它。

标签: android button android-linearlayout layout-inflater


【解决方案1】:

可能的方法是在添加布局和按钮时设置标签值。根据标签将布局放置在地图中。然后在单击按钮时使用标签从地图中获取布局并将其删除。标签是一个内部标记,从未在 UI 上显示。

【讨论】:

  • 你好凯迪尔。谢谢你的回复。你能告诉我一个例子,我怎么能用标签值做到这一点?谢谢
  • 你好,凯迪尔。我解决了我的问题。我使用了标签值。谢谢你的帮助。 :)
【解决方案2】:

您正在寻找的是ExpandableListView。无需重新发明轮子。

【讨论】:

  • 嗨克里斯蒂弗。感谢您的回复。你能告诉我一个如何使用 ExpandableListView 做到这一点的例子吗?谢谢
  • 这里有一个很好的教程:coderzheaven.com/2011/04/10/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多