选择项

时间:2015-09-15 04:59:37

标签: android android-layout android-spinner

我有一个奇怪的问题。我在布局中添加了spinner。当我在spinner中选择一个项目时,spinner会在布局中向下移动,导致页面中出现垂直滚动。我不希望发生这种情况,因为这会导致我的页面上的按钮在滚动之前显示在屏幕上较少。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="20dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/id_gwp_configuration_productImage"
            android:layout_width="@dimen/product_collections_image_width"
            android:layout_height="@dimen/product_collections_image_height"
            android:scaleType="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="10dp" >

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
                android:id="@+id/id_gwp_configuration_titleTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:ellipsize="end"
                android:lineSpacingExtra="8dp"
                android:maxLines="3"
                android:paddingBottom="4dp"
                android:singleLine="false"
                android:text="@string/pdp_title"
                android:textColor="@android:color/black"
                android:textSize="12sp" />

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
                android:id="@+id/id_gwp_configuration_salePriceTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:text="@string/pdp_sale_price"
                android:textColor="@color/pdp_red_sale_price"
                android:textSize="18sp"
                android:textStyle="bold" />

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
                android:id="@+id/id_gwp_configuration_regularPriceTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:text="@string/pdp_regular_price"
                android:textColor="@color/pdp_grey_original_price"
                android:textSize="12sp" />

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
                android:id="@+id/id_gwp_configuration_skuCodeTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:text="@string/pdp_regular_price"
                android:textColor="@color/pdp_grey_original_price"
                android:textSize="12sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
    android:id="@+id/id_gwp_configuration_offerTxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:maxLines="2"
    android:paddingBottom="20dp"
    android:textColor="@color/pdp_green_offer"
    android:textSize="12sp"
    android:visibility="gone" />

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
    android:id="@+id/id_gwp_configuration_short_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lineSpacingExtra="8dp"
    android:maxLines="2"
    android:text="Description text"
    android:textSize="12sp" />

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
    android:id="@+id/id_gwp_configuration_show_more"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="@string/show_more"
    android:textColor="@android:color/holo_blue_bright"
    android:textSize="12sp" />

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
    android:id="@+id/id_gwp_configuration_long_descTxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lineSpacingExtra="8dp"
    android:text="Description text"
    android:textSize="12sp"
    android:visibility="gone" />

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
    android:id="@+id/id_gwp_configuration_show_less"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="@string/show_less"
    android:textColor="@android:color/holo_blue_bright"
    android:textSize="12sp"
    android:visibility="gone" />

<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/product_details_swatch_images" />

<LinearLayout
    android:id="@+id/id_gwp_configuration_swatchShowLessLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone" >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@drawable/product_detail_black_divider" />

    <com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
        android:id="@+id/id_gwp_configuration_swatchShowLessTxt"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="@android:color/white"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/pdp_show_fewer_colors"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:id="@+id/id_gwp_configuration_sizeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp"
    android:visibility="gone" >

    <Spinner
        android:id="@+id/id_gwp_configuration_sizeSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp" />

    <LinearLayout
        android:id="@+id/id_gwp_configuration_sizeGuideLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
            android:id="@+id/id_gwp_configuration_sizeNotSureTxt"
            style="bold"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:text="@string/pdp_not_sure_about_size"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="bold" />

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

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
                android:id="@+id/id_gwp_configuration_sizeCheckOutTxt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:text="@string/pdp_check_out_guide"
                android:textColor="@android:color/black"
                android:textSize="12sp" />

            <com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
                android:id="@+id/id_gwp_configuration_sizeGuide"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:textColor="@android:color/black"
                android:textSize="12sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBookTextView
    android:id="@+id/id_gwp_configuration_quantityTxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="4dp"
    android:paddingTop="12dp"
    android:text="@string/pdp_quantity"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    android:visibility="gone" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="4dp" >

    <Button
        android:id="@+id/id_gwp_configuration_qtyDownBtn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/product_detail_black_border"
        android:text="@string/pdp_qty_minus"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:visibility="gone" />

    <EditText
        android:id="@+id/id_gwp_configuration_qtyValue"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/product_detail_black_border"
        android:gravity="center"
        android:inputType="number"
        android:text="1"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:visibility="gone" />

    <Button
        android:id="@+id/id_gwp_configuration_qtyUpBtn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/product_detail_black_border"
        android:text="@string/pdp_qty_plus"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:visibility="gone" />

    <com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldButton
        android:id="@+id/id_gwp_configuration_addGiftToBagBtn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:background="@color/pdp_add_to_bag_disable"
        android:text="@string/gwp_add_gift_to_bag"
        android:textColor="@android:color/white"
        android:textSize="12sp" />
</LinearLayout>

<com.kohls.mcommerce.opal.framework.view.component.views.GothamBoldTextView
    android:id="@+id/id_gwp_configuration_removeGift"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:paddingBottom="4dp"
    android:text="@string/pdp_remove_gift"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    android:visibility="gone" />

我在代码中使用spinner

/**
 * Set spinner to display size of the mProduct.
 */
private void setProductSizeSpinner() {

    if (mProduct != null) {
        List<String> sizeList = new ArrayList<String>();

        if (mProduct.getSKUS() != null) {
            ArrayList<String> sizeListSet = null;
            PriceColor priceColor = new PriceColor();
            if (mSelectedColor == null) {
                if (!mColorToSizeMap.isEmpty()) {
                    Iterator<Entry<PriceColor, ArrayList<String>>> enteries = mColorToSizeMap.entries();
                    if (enteries.hasNext()) {
                        Entry<PriceColor, ArrayList<String>> entry = enteries.next();
                        priceColor = entry.getKey();
                        if (entry.getKey() != null) {
                            mSelectedColor = entry.getKey().color;
                        } else {
                            mSelectedColor = new String();
                        }
                        sizeListSet = entry.getValue();
                        mIsColorRequired = true;
                        mSwatchColorNameLayout.setVisibility(View.VISIBLE);
                        mTextSwatchColorName.setText(" " + mSelectedColor);
                    }
                }
            } else {
                com.kohls.mcommerce.opal.common.util.Logger.debug("ProductDetails",
                        "Price : " + mSelectedPrice);
                if (mSelectedPrice == null || mSelectedPrice.equalsIgnoreCase("")) {
                    priceColor.price = getSKUPrice(mSku);
                } else {
                    priceColor.price = mSelectedPrice;
                }
                priceColor.color = mSelectedColor;
                if (mSwatchImagesList != null && !mSwatchImagesList.isEmpty()) {
                    sizeListSet = mColorToSizeMap.getValue(priceColor);
                } else if (!mColorToSizeMap.isEmpty()) {
                    Collection<ArrayList<String>> allSizesList = mColorToSizeMap.getAllValues();
                    sizeListSet = new ArrayList<String>();
                    for (ArrayList<String> newSizesList : allSizesList) {
                        sizeListSet.addAll(newSizesList);
                    }
                }
            }

            if (mCrossedColorsIds != null && !mCrossedColorsIds.isEmpty()) {
                clearCrossedSwatchImages();
            }
            if (sizeListSet != null) {
                sizeList = removeOutofStockSizes(sizeListSet, priceColor);
            }
            if (sizeList != null && sizeList.size() > 0) {
                mIsSizeRequired = true;
                sizeList.add(0, mContext.getString(R.string.pdp_select_size));

                mSpinnerProductSizeLayout.setVisibility(View.VISIBLE);
                mSpinnerProductSize.setVisibility(View.VISIBLE);
                dropDownAdapterSize = new ArrayAdapter<String>(mContext,
                        R.layout.spinner_text_view,
                        sizeList) {

                    @Override
                    public View
                            getDropDownView(int position, View convertView, ViewGroup parent) {

                        View view = null;

                        TextView textSize = new TextView(getContext());

                        if (position == 0) {
                            textSize.setHeight(0);
                            textSize.setVisibility(View.GONE);
                            view = textSize;
                        } else {
                            // Pass convertView as null to prevent
                            // reuse of special case views
                            view = super.getDropDownView(position, null, parent);
                        }
                        return view;
                    }
                };

                mSpinnerProductSize.setAdapter(dropDownAdapterSize);

                // Select last selected value of size.
                if (mSelectedSize != null && mSkuMap.size() > 1) {
                    int spinnerPosition = dropDownAdapterSize.getPosition(mSelectedSize);
                    if (spinnerPosition == -1) {
                        mSpinnerProductSize.setSelection(0);
                        mIsSizeRequired = false;
                        mSelectedSize = null;
                        setButtonState(false);
                    } else {
                        mSpinnerProductSize.setSelection(spinnerPosition);
                    }
                }
                mSpinnerProductSize.setOnItemSelectedListener(mSizeSpinnerItemSelectedListener);
            } else {
                mIsSizeRequired = false;
                mSpinnerProductSize.setSelection(0);
                mSpinnerProductSize.setVisibility(View.INVISIBLE);
            }
        }
    }
}

https://www.dropbox.com/s/50xw0szs96c8ql0/Screenshot_2015-09-05-14-40-17.png?dl=0

https://www.dropbox.com/s/e8x066r9e4iz3mu/Screenshot_2015-09-05-14-42-48.png?dl=0

正如您在上面的屏幕截图中看到的那样,我指的是“添加礼物到包”按钮被隐藏。

2 个答案:

答案 0 :(得分:1)

您只需指定微调器的高度

答案 1 :(得分:0)

您能否请在您的微调器上应用此选项并检查android:spinnerMode="dropdown"您可以将其作为对话框进行操作android:spinnerMode="dialog"