0

Spinner を持っていますが、プロンプト メッセージが無視されます。理由がわかりません。

私のレイアウト:

<Spinner
       android:id="@+id/spinner"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="15dp"
       android:prompt="@string/age"
       android:drawSelectorOnTop="true"
       android:background="@drawable/spinner_bg" />

私のコード:

spinner = (Spinner) findViewById(R.id.spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getApplicationContext().getResources().getStringArray(R.array.spinnerArray));
    spinner.setAdapter(adapter);
    adapter.setDropDownViewResource(R.layout.spinner_item);

SpinnerAdapter :

public class SpinnerAdapter extends ArrayAdapter<String>{

public SpinnerAdapter(Context context, int textViewResourceId,
        String[] objects) {
    super(context, textViewResourceId, objects);
}


public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);

    Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
    ((TextView) v).setTypeface(externalFont);

    return v;
}


public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
    View v =super.getDropDownView(position, convertView, parent);

    Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
    ((TextView) v).setTypeface(externalFont);

    return v;
}

}

そして spinner_item のレイアウト:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:minHeight="38dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="18sp" />

何か考えはありますか?

4

1 に答える 1

3

I think you are misunderstanding what android:prompt does... it doesn't set the text in the closed spinner, it sets the header/title of the open spinner.

Closed spinner:     Spinner Item 1

Open Spinner:       Android Spinner Prompt
                    ----------------------
                    Spinner Item 1
                    Spinner Item 2

There are a couple of ways to put the prompt in the closed spinner display. One way is to insert dummy data that will load into your spinner in the first position and then have your listener ignore that if it's selected (I don't like this as it puts junk in your data store).

Another option is to create a custom spinner adapter to insert the prompt as the first entry of the spinner (I like this one as it keeps the prompt in the code and keeps your data what it should be... data).

Hope this helps! Good luck!

于 2012-05-07T06:53:19.507 に答える