2012-05-06 54 views
0

我有一个微调,但提示消息被忽略。我找不到原因。android:忽略提示

我的布局:

<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" /> 

你有什么想法吗?

+0

'age'是否存在于你的strings.xml中? – Barak

+0

是的,我已经尝试了几个字符串 – g123k

+0

你是否知道这个问题?是/它真的没有工作,或者只是不在你期望的地方(像我理论化的那样)? – Barak

回答

3

认为您误解了android:prompt的作用......它没有在关闭的微调框中设置文本,它设置了打开微调框的标题/标题。

Closed spinner:  Spinner Item 1 

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

有几种方法可将提示放入关闭的微调器显示中。一种方法是在第一个位置插入将加载到您的微调器中的虚拟数据,然后让您的监听器忽略,如果它被选中(我不喜欢这样做,因为它会将垃圾放入数据存储区)。

另一种选择是创建一个自定义的微调控制器适配器来插入提示符作为微调控制器的第一项(我喜欢这个,因为它在代码中保持提示并保持你的数据应该是数据) 。

希望这会有所帮助!祝你好运!