开发者

Hide text of Android spinner

开发者 https://www.devze.com 2023-02-09 19:01 出处:网络
I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is,

I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the 开发者_Python百科selected item.


Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);  
spin.setOnItemSelectedListener(new OnItemSelectedListener() {  
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
// hide selection text  
((TextView)view).setText(null);  
// if you want you can change background here  
}  
  public void onNothingSelected(AdapterView<?> arg0) {}  
 });


Create ghost_text.xml in your layouts folder with this text:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textColor="@android:color/transparent" />

Then use that file in your adapter like this:

    ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this, 
        R.layout.ghost_text, your_list);
    your_spinner.setAdapter(your_adapter);

You should also be able to use this technique with other resource-based adapters.


Probably you can define a xml-layout file, without any textview in it, and supply it to the adapter for the spinner.

e.g. empty_spinner_item.xml

<ImageView xmlns:android="http..."
    android:layout_width=".." 
/>

and then use any adapter:

spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号