我尝试使用ActionBarSherlock在ActionBar外部创建IcsSpinner。我使用了来自Dropdown Spinner outside of actionbar? (IceCream Sandwich style, w/ActionBarSherlock)的信息。它看起来不错,这就是我要找的东西。我的问题是我的微调器只显示第一个选项而且不可点击。无法访问下拉菜单,也从不调用方法getDropDownView。
HoloSpinner.java:
import android.content.Context;
import android.util.AttributeSet;
import com.actionbarsherlock.internal.widget.IcsSpinner;
public class HoloSpinner extends IcsSpinner {
public HoloSpinner(Context context, AttributeSet attrs) {
super(context, attrs, com.actionbarsherlock.R.attr.actionDropDownStyle);
}
public HoloSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
HoloSpinnerAdapter.java:
package pl.tmmk.organizer.studencki;
import android.content.Context;
import android.database.DataSetObserver;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class HoloSpinnerAdapter<T> extends ArrayAdapter<T> implements SpinnerAdapter {
private LayoutInflater layoutInflater;
private T[] array;
public HoloSpinnerAdapter(Context context, int res, T[] objects){
super(context, res, objects);
layoutInflater = LayoutInflater.from(context);
array = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("HINT", "getView");
final TextView filterName;
if (convertView == null) {
filterName = (TextView) layoutInflater.inflate(R.layout.sherlock_spinner_item, parent, false);
} else {
filterName = (TextView) convertView;
}
filterName.setText(getItem(position).toString());
return filterName;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Log.d("HINT", "getDropDownView");
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).toString());
return filterName;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return array.length;
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return array[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
xml文件的一部分:
<com.xxx.yyyy.zzzzz.HoloSpinner
android:layout_marginTop="4dp"
android:id="@+id/event_type_spinner"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:textAllCaps="true"
android:background="@drawable/abs__spinner_ab_holo_dark"
android:textColor="#000000"
android:gravity="center"/>
微调器初始化:
String[] eventTypeArray = getResources().getStringArray(R.array.event_type_array);
HoloSpinnerAdapter<String> spinnerAdapter = new HoloSpinnerAdapter<String>(this, R.layout.sherlock_spinner_item, eventTypeArray);
HoloSpinner spinner = (HoloSpinner) findViewById(R.id.event_type_spinner);
spinner.setAdapter(spinnerAdapter);
我的目标是即使在较旧的Android版手机上也能获得ICS外观。我不知道我做错了什么。谢谢你的帮助。
答案 0 :(得分:0)
在清单中设置样式非常重要
<activity android:name="fr.example.demo.MyActivity" android:theme="@style/Theme.Sherlock"></activity>
或修改style.xml中的主题样式
<style name="AppTheme" parent="@style/Theme.Sherlock">
</style>