Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

android - Dropdown Spinner outside of actionbar? (IceCream Sandwich style, w/ActionBarSherlock)

Is there a way to create a Dropdown Spinner for Android 2.3.3? I am using ActionbarSherlock.

Here is an Example of what I mean:

enter image description here

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As it stands, you're in luck. It can be done with ActionBarSherlock and it works with versions pre-4.0 . However, I'm not 100% sure Jake Wharton will want us to use it like this, since it's not exactly "public api", AFAIK (I've meant to ask). Anyway, you have to first create your own class to extend from the ActionBarSherlock class:

public class MyIcsSpinner extends IcsSpinner {

  public MyIcsSpinner(Context context, AttributeSet attrs) {
    super(context, attrs, com.actionbarsherlock.R.attr.actionDropDownStyle);

  }

  public MyIcsSpinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

  }
}

To include it in a layout:

<com.blah.blah.blah.MyIcsSpinner
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:textAllCaps="true"
    android:background="@drawable/abs__spinner_ab_holo_light"
    android:textColor="#000000"
    android:gravity="center"/>

Now you have to create a custom SpinnerAdapter, and you need to override the following methods to get the proper look and feel:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final TextView filterName;
    if (convertView == null) {
      filterName = (TextView) layoutInflater.inflate(R.layout.filter_item, parent, false);
    } else {
      filterName = (TextView) convertView;
    }

    filterName.setText(getItem(position));
    return filterName;
  }

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    final TextView filterName;
    if (convertView == null) {
      filterName = (TextView) layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false);
      filterName.setEllipsize(TruncateAt.END);
    } else {
      filterName = (TextView) convertView;
    }

    filterName.setText(getItem(position));
    return filterName;
  }

YMMV, esp. regarding the themes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...