【问题标题】:Generating View Dynamically动态生成视图
【发布时间】:2014-11-03 19:25:20
【问题描述】:

我得到了这个屏幕,其中有一些要创建的静态视图和一些动态视图。 我希望创建无限数量的视图,我认为动态是前进的方向

现在我只需要创建带有图像按钮的顶部列、带有客户端名称的编辑文本和带有 xml 中现金的微调器(这是我目前拥有的布局)现在我想要如果用户单击购物车图标视图动态生成产品名称、数量和价格,然后通过xml生成底部视图

喜欢这个

如您所见,有两行产品,数量和价格。目前这是通过 xml 完成的,someboy 可以帮助我如何在这样的现有 xml 布局之间生成和排列包含数量、价格和产品名称的行?

注意:主要布局是相对布局,包含要动态生成的行的布局是这样的

                 <LinearLayout
            android:id="@+id/laygroup0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_below="@+id/lvpaymode"
            android:gravity="center"
            android:orientation="vertical" >

            <AutoCompleteTextView
                android:id="@+id/autoproduct0"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:drawableLeft="@drawable/ic_shopping_cart_black_24dp"
                android:ems="10"
                android:hint="@string/strproductname"
                android:padding="10dp"
                android:singleLine="true" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal" >

                <EditText
                    android:id="@+id/autoquantity0"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:drawableLeft="@drawable/ic_local_mall_black_24dp"
                    android:ems="10"
                    android:hint="@string/strquantity"
                    android:inputType="numberDecimal"
                    android:padding="10dp"
                    android:singleLine="true" >
                </EditText>

                <EditText
                    android:id="@+id/autoprice0"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:drawableLeft="@drawable/ic_attach_money_black_24dp"
                    android:ems="10"
                    android:hint="@string/strprice"
                    android:inputType="numberDecimal"
                    android:padding="10dp"
                    android:singleLine="true" >
                </EditText>
            </LinearLayout>
        </LinearLayout>

【问题讨论】:

  • 你可以扩展上述布局的实例并将其添加到父视图,如下所示:parent.addView(child);
  • 并且每次点击添加购物车的按钮时都这样做?你能举个例子或链接吗
  • 是的,每次点击添加购物车 btn 时您都会这样做

标签: android xml android-layout android-edittext dynamically-generated


【解决方案1】:

我不能给出完整的代码,因为我是用手机写的,但我可以给你更多的指点。 在主布局中以 xml 格式创建父布局。如果它是一个 LinearLayout 会容易得多(任何其他布局都将更难处理,更多的关注和更多的代码)。 然后使用 findViewById 在 java 代码中引用它的父级: LinearLayout parent = findViewById(R.layout....); 您将使用父视图在其中添加子视图。

接下来设置一个连接了 onClick 事件的按钮。在 onClick 事件中,您将膨胀子布局: View child = LayoutInflater.from(context).inflate(R.layout.childxml);

接下来使用 TextView tv = child.findViewById(... 查找其中包含的所有视图等。如果您有按钮,请在此处设置其他按钮事件。 最后,您可以将孩子添加到父母: parent.addView(child);

如果父级是线性布局且方向设置为垂直,则所有视图将在另一个下方很好地排列。 如果您添加许多视图,则可能会超出手机屏幕允许的范围,并且无法滚动。您可以通过在 xml 中使用 ScrollView 包装父线性布局来克服这个问题。现在它会很好地工作。 希望对您有所帮助。

【讨论】:

  • 谢谢,但我需要它就像你看到的那样的布局,父级确实是线性布局,但由于会添加视图,我使用线性布局顶部放置那些固定的图像按钮(它们应该全部可见时间)和 Total 的底层是滚动视图结束的地方,因此中间的所有内容都是可滚动的,重复布局是具有两个子项的 LinearLayout 一个自动完成和一个具有其他两个子项的线性布局。
  • 是的,您将拥有将被修复的顶部和底部布局。在中间,它将是动态添加的子项的父容器。
  • 让我试试你的方法
  • 每次单击添加按钮时它都不起作用,似乎视图只创建了一次。我需要的是有人看到他已经添加的订单
  • 学会描述你想要什么可以帮助别人给你你正在寻找的答案。太糟糕了,现在才清楚你想要达到什么。在这种情况下,您将需要使用列表视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多