微调框简单项目模板获得不同的大小

时间:2018-12-07 12:00:54

标签: java android android-spinner

我正在使用数组适配器在微调器上填充字符串。但是它不能呈现正确的尺寸。请帮助

enter image description here enter image description here

布局代码

<Spinner
                android:id="@+id/reg_state"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="24dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/constraintLayout2" />

            <Spinner
                android:id="@+id/reg_batch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:layout_marginTop="6dp"
                android:layout_marginEnd="24dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView9" />

Java代码

ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, batchList);
                arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                batch.setAdapter(arrayAdapter1);

                ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, stateList);
                arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                state.setAdapter(arrayAdapter2);

2 个答案:

答案 0 :(得分:1)

为此,您可以如下使用spinner_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerMode"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textSize="@dimen/textSizeDefault"
    android:paddingLeft="@dimen/margin_small"
    android:paddingTop="@dimen/edtSpnrPadding"
    android:paddingBottom="@dimen/edtSpnrPadding"
    android:includeFontPadding="true"
    android:lineSpacingExtra="2dp"
    android:text="@string/tvscreditservice"
    android:textAllCaps="false"
    android:textAppearance="@style/TextAppearance"
    android:textColor="@color/colorPrimary"
    android:textAlignment="inherit"/>

以Java方式访问

 adapter = new ArrayAdapter<CommonPojo>(this, R.layout.spinner_item, list);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        spinner.setAdapter(adapter);

activity.xml将旋转器更改为下方的旋转器。

<android.support.v7.widget.AppCompatSpinner
                        android:id="@+id/stateSpinner"
                        android:layout_width="0dp"
                        android:layout_weight="50"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/edtSpnrPadding"
                        android:hint="@string/state"
                        android:textColor="@color/colorPrimary"
                        android:textAppearance="@style/TextAppearance"
                        android:paddingLeft="2dp"
                        android:paddingRight="2dp"
                        android:paddingTop="@dimen/edtSpnrPadding"
                        android:paddingBottom="@dimen/edtSpnrPadding"
                        style="@style/Widget.AppCompat.Spinner.Underlined"
                        app:backgroundTint="@color/colorText"/>

所有微调器项目的结果将保持不变。

答案 1 :(得分:1)

如下更改代码

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item,areas);
state.setAdapter(adapter);

并在布局文件夹中创建spinner_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:singleLine="true"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight" />

在测试代码之前清理并构建。