根据地图大小在特定位置的现有 LinearLayout 内添加视图
Adding views inside an existing LinearLayout in a specific place according to map size
我有 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=".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>
看起来像这样 -
我想要实现的是在 "availability" textview 下面我想添加 textview 以与可用性相同的方式显示信息。
到目前为止我尝试了什么 -
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);
}
它在线性布局的底部创建视图,因此我缺少将它放在特定位置的选项。我怎样才能做到这一点?
在您有评论的地方添加 LinearLayout
<!-- This is where I want to add the dynamic views -->
并在 ViewGroup
中添加 TextView,而不是 nestedLinearLayout
至于
so I am missing the option to put it at a specific place
您可以使用第二个 int
参数调用 addView
,该参数是您要插入视图的索引
我有 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=".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>
看起来像这样 -
我想要实现的是在 "availability" textview 下面我想添加 textview 以与可用性相同的方式显示信息。
到目前为止我尝试了什么 -
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);
}
它在线性布局的底部创建视图,因此我缺少将它放在特定位置的选项。我怎样才能做到这一点?
在您有评论的地方添加 LinearLayout
<!-- This is where I want to add the dynamic views -->
并在 ViewGroup
中添加 TextView,而不是 nestedLinearLayout
至于
so I am missing the option to put it at a specific place
您可以使用第二个 int
参数调用 addView
,该参数是您要插入视图的索引