任何人都可以告诉我,如何在Fragment中将Spinner添加到操作栏?
答案 0 :(得分:3)
我使用以下步骤解决了这个问题
步骤1
在布局文件夹中创建xml文件(考虑布局名称是 spinnerwound.xml )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#F79D27"
android:orientation="vertical">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spin"/>
</LinearLayout>
步骤2
在Fragment中写这样的
((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflator.inflate(R.layout.spinnerwound, null);
Spinner s = (Spinner) vi.findViewById(R.id.spin);
spinnerlist = new ArrayList<String>();
spinnerlist.add(Item1);
spinnerlist.add(Item2);
spinneradapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line, spinnerlist);
s.setAdapter(spinneradapter);
((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(vi);
spinneradapter.notifyDataSetChanged();
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Do what ever you want
}