首页 > 解决方案 > 根据地图大小在特定位置的现有 LinearLayout 中添加视图

问题描述

我有一个 XML 文件的以下部分 -

 <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_product_page_top_product_layout">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/activity_product_page_nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <LinearLayout
                android:id="@+id/activity_product_page_nested_linear_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingBottom="@dimen/marketplace_padding_15dp">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/very_light_grey" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/activity_product_page_brands_breadcrumbs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginLeft="@dimen/marketplace_14dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/breadcrumbs_brands"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/activity_product_page_vendor_breadcrumbs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_toEndOf="@id/activity_product_page_brands_breadcrumbs"
                        android:layout_toRightOf="@id/activity_product_page_brands_breadcrumbs"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/products_breadcrumbs_unknown_vendor"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/activity_product_page_product_breadcrumbs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_toEndOf="@id/activity_product_page_vendor_breadcrumbs"
                        android:layout_toRightOf="@id/activity_product_page_vendor_breadcrumbs"
                        android:ellipsize="end"
                        android:fontFamily="@font/noto_sans"
                        android:maxLines="1"
                        android:text="@string/activity_product_page_unknown_product"
                        android:textColor="#979797" />


                </RelativeLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="@dimen/marketplace_padding_15dp"
                    android:background="@color/very_light_grey" />


                <android.support.v4.view.ViewPager
                    android:id="@+id/activity_product_page_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:paddingStart="40dp"
                    android:paddingLeft="40dp"
                    android:paddingEnd="40dp"
                    android:paddingRight="40dp"
                    tools:layout_height="300dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/marketplace_padding_15dp"
                    android:background="@color/very_light_grey"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/activity_product_page_product_title_textview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        android:textSize="20sp"
                        android:textStyle="bold"
                        tools:text="Products long title" />

                    <TextView
                        android:id="@+id/activity_product_page_product_short_description_textview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        tools:text="Product short description" />

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp">

                        <TextView
                            android:id="@+id/activity_product_page_sold_by"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_sold_by" />

                        <TextView
                            android:id="@+id/activity_product_page_vendors_name_textview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignTop="@id/activity_product_page_sold_by"
                            android:layout_marginStart="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_toEndOf="@+id/activity_product_page_sold_by"
                            android:layout_toRightOf="@+id/activity_product_page_sold_by"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_unknown"
                            android:textStyle="bold"
                            tools:text="Vendors name, " />

                        <TextView
                            android:id="@+id/activity_product_page_fulfilled_by_hard_coded_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/activity_product_page_vendors_name_textview"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_fulfilled_by" />

                        <TextView
                            android:id="@+id/activity_product_page_fulfilled_by_textview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignTop="@+id/activity_product_page_fulfilled_by_hard_coded_text"
                            android:layout_marginStart="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_toEndOf="@+id/activity_product_page_fulfilled_by_hard_coded_text"
                            android:layout_toRightOf="@+id/activity_product_page_fulfilled_by_hard_coded_text"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_unknown"
                            android:textStyle="bold"
                            tools:text="Project Verte." />

                        <TextView
                            android:id="@+id/activity_product_page_sku_textview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_alignParentRight="true"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_sku_unknown"
                            tools:text="SKU: K004" />

                    </RelativeLayout>

                    <TextView
                        android:id="@+id/activity_product_page_product_price_textview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_fetching_price"
                        android:textSize="20sp"
                        android:textStyle="bold"
                        tools:text="$35.00" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_additional_tax_may_apply_on_checkout" />

                    <RelativeLayout
                        android:id="@+id/activity_product_page_availability_view_group"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp">

                        <TextView
                            android:id="@+id/activity_product_page_availability_hardcoded"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/marketplace_14dp"
                            android:layout_marginEnd="@dimen/marketplace_14dp"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_availability" />

                        <TextView
                            android:id="@+id/activity_product_page_availability_textview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignTop="@+id/activity_product_page_availability_hardcoded"
                            android:layout_toEndOf="@+id/activity_product_page_availability_hardcoded"
                            android:layout_toRightOf="@+id/activity_product_page_availability_hardcoded"
                            android:fontFamily="@font/noto_sans"
                            android:text="@string/activity_product_page_unknown"
                            android:textColor="@color/red_delete"
                            tools:text="Out of stock" />

                    </RelativeLayout>

                    <!-- This is where I want to add the dynamic views -->

                    <Button
                        android:id="@+id/activity_product_page_add_to_cart_button"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:background="@color/color_black"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_add_to_cart"
                        android:textColor="@color/white" />

                    <Button
                        android:id="@+id/activity_product_page_buy_now_button"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:background="@color/color_black"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_buy_now"
                        android:textColor="@color/white" />

                    <TextView
                        android:id="@+id/activity_product_page_share_textview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_share" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/marketplace_14dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="@dimen/marketplace_14dp"
                        android:fontFamily="@font/noto_sans"
                        android:text="@string/activity_product_page_related_products"
                        android:textSize="22sp"
                        android:textStyle="bold" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/activity_product_page_related_recycleview"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        app:layoutManager="android.support.v7.widget.GridLayoutManager"
                        tools:listitem="@layout/fragment_marketplace_products_row_item" />

                    <View
                        android:id="@+id/activity_product_page_bottom_view"
                        android:layout_width="match_parent"
                        android:layout_height="70dp"
                        android:layout_marginTop="@dimen/marketplace_padding_15dp"
                        android:layout_marginBottom="60dp"
                        android:background="@color/light_black"
                        android:visibility="gone"
                        tools:visibility="visible" />


                </LinearLayout>

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

    </android.support.constraint.ConstraintLayout>

看起来像这样 -

在此处输入图像描述

我想要实现的是在“可用性”文本视图下方我想添加文本视图,以与可用性相同的方式显示信息。

到目前为止我尝试过的 -

for (String key : selectedProductAttributesMap.keySet()) {
            TextView textView = new TextView(this);
            textView.setText(key);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(14,15,14,0);
            textView.setLayoutParams(params);
            nestedLinearLayout.addView(textView);
        }

它在线性布局的底部创建视图,所以我错过了将其放在特定位置的选项。我怎样才能做到这一点?

标签: androidviewdynamic-view

解决方案


添加一个LinearLayout您有评论的地方<!-- This is where I want to add the dynamic views -->并在其中添加 TextViews ViewGroup,而不是nestedLinearLayout

至于

所以我错过了将它放在特定位置的选项

您可以addView使用第二个int参数调用,该参数是您希望插入视图的索引


推荐阅读