运行应用程序时不会显示Spinner

时间:2016-09-21 17:43:31

标签: android android-spinner

我正在尝试使用微调器。我和旋转器一起有其他观点。除了微调器之外,所有其他视图都显示并正常工作。在执行时,微调器根本不可见。但它在设计模式下显示,但在执行时不显示。

我的XML如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background"
    tools:context="com.example.kc.sample.HomePage">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

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

            <android.support.design.widget.TextInputLayout
                style="@style/fontStyle"
                android:id="@+id/input_layout_registerName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:inputType="textCapSentences"
                    android:textSize="15dp"
                    android:paddingStart="@dimen/activity_horizontal_margin"
                    android:textColor="#274A72"
                    android:id="@+id/editRegisterName"
                    android:hint="@string/Name"
                    android:singleLine="true"
                    android:background="@drawable/edit_text_border"
                    android:lines="1"
                    android:maxLength="64"/>
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                style="@style/fontStyle"
                android:id="@+id/input_layout_registerDOB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:inputType="textCapSentences"
                    android:textSize="15dp"
                    android:paddingStart="@dimen/activity_horizontal_margin"
                    android:focusableInTouchMode="false"
                    android:textColor="#274A72"
                    android:id="@+id/editRegisterDOB"
                    android:hint="@string/DOB"
                    android:singleLine="true"
                    android:background="@drawable/edit_text_border"
                    android:lines="1"
                    android:maxLength="64"/>
            </android.support.design.widget.TextInputLayout>

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*"
                android:shrinkColumns="*">
                <TableRow>
                    <android.support.design.widget.TextInputLayout
                        style="@style/fontStyle"
                        android:id="@+id/input_layout_GenderMale"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <RadioButton
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:textSize="15dp"
                            android:paddingStart="@dimen/activity_horizontal_margin"
                            android:textColor="#274A72"
                            android:id="@+id/radioGenderMale"
                            android:text="@string/Male"
                            android:background="@drawable/edit_text_border"
                            android:onClick="checkMaleMethod"
                        />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        style="@style/fontStyle"
                        android:id="@+id/input_layout_GenderFemale"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <RadioButton
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_marginRight="10dp"
                            android:textSize="15dp"
                            android:paddingStart="@dimen/activity_horizontal_margin"
                            android:textColor="#274A72"
                            android:id="@+id/radioGenderFemale"
                            android:text="@string/Female"
                            android:background="@drawable/edit_text_border"
                            android:onClick="checkFemaleMethod"
                        />
                    </android.support.design.widget.TextInputLayout>
                </TableRow>
            </TableLayout>

            <android.support.design.widget.TextInputLayout
                style="@style/fontStyle"
                android:id="@+id/input_layout_registerEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:inputType="textCapSentences"
                    android:textSize="15dp"
                    android:paddingStart="@dimen/activity_horizontal_margin"
                    android:textColor="#274A72"
                    android:id="@+id/editRegisterEmail"
                    android:hint="@string/email"
                    android:singleLine="true"
                    android:background="@drawable/edit_text_border"
                    android:lines="1"
                    android:maxLength="64"/>
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                style="@style/fontStyle"
                android:id="@+id/input_layout_registerMobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:inputType="number"
                    android:layout_height="40dp"
                    android:id="@+id/editRegisterPhone"
                    android:textSize="15dp"
                    android:paddingStart="@dimen/activity_horizontal_margin"
                    android:textColor="#274A72"
                    android:hint="@string/Phone"
                    android:singleLine="true"
                    android:background="@drawable/edit_text_border"
                    android:lines="1"
                    android:maxLength="10"/>
            </android.support.design.widget.TextInputLayout>

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*"
                android:shrinkColumns="*">
                <TableRow>
                    <android.support.design.widget.TextInputLayout
                        style="@style/fontStyle"
                        android:id="@+id/input_layout_statusSingle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <RadioButton
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:textSize="15dp"
                            android:paddingStart="@dimen/activity_horizontal_margin"
                            android:textColor="#274A72"
                            android:id="@+id/radioStatusSingle"
                            android:text="@string/Single"
                            android:background="@drawable/edit_text_border"
                            android:onClick="checkStatusSingleMethod"
                            />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        style="@style/fontStyle"
                        android:id="@+id/input_layout_statusMarried"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <RadioButton
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_marginRight="10dp"
                            android:textSize="15dp"
                            android:paddingStart="@dimen/activity_horizontal_margin"
                            android:textColor="#274A72"
                            android:id="@+id/radioStatusMarried"
                            android:text="@string/Married"
                            android:background="@drawable/edit_text_border"
                            android:onClick="checkStatusMarriedMethod"
                            />
                    </android.support.design.widget.TextInputLayout>
                </TableRow>
            </TableLayout>

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:prompt="@string/spinner_title"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtErrorMessage"
                android:textAlignment="center"
                android:textSize="15dp"
                android:textColor="#ff0000"/>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

我对应的java代码如下:

public class HomePage extends AppCompatActivity implements View.OnClickListener, OnItemSelectedListener {

    private EditText editName, editDateofBirth, editMail, editMobileNumber, editDate;
    private RadioButton radioMale, radioFemale, radioStatusSingle, radioStatusMarried;
    private Button btnPrevious, btnNext;
    Dialog result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        //Few other view declaration here

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);

        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Four");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);           dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

    }
@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String item = adapterView.getItemAtPosition(i).toString();
        Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

如果作为单独的单个项目编写的相同Spinner代码工作正常并且符合预期,但在上面的代码片段中包含相同内容将不会在执行时显示微调器。任何人都可以指导我如何使它工作?提前致谢

1 个答案:

答案 0 :(得分:0)

不推荐使用Fill属性,请使用:

<Spinner
   android:id="@+id/spinner"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:prompt="@string/spinner_title"
            />