我正在尝试在操作栏中放置一个下拉/下拉搜索栏。我认为这样的事情OnCreateOptionsMenu()
会起作用:
SubMenu subMenu = menu.addSubMenu("DropTest");
MenuItem seekBarDrop = subMenu2.add("SeekBar");
SeekBar seekBar = new SeekBar(this);
seekBarDrop.setActionView(seekBar);
seekBarDrop.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItem subMenuItem = subMenu2.getItem();
subMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
但它失败得很惨。我还尝试将其破解为微调器适配器(使用 IcsSpinner 进行 actionbarsherlock):
ArrayAdapter<CharSequence> seekAdapter =
new ArrayAdapter<CharSequence>(context,
R.layout.spinner_subtitled_octave,
android.R.id.text1, new String[]{""});
seekAdapter.setDropDownViewResource(R.layout.seekbar);
MenuItem itemX = menu.add("SeekBar").setActionView(R.layout.spinner_ics);
itemX.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
IcsSpinner spinBar = (IcsSpinner) itemX.getActionView();
spinBar.setAdapter(seekAdapter);
/*
* This next part doesn't crash, but it also doesn't work
*/
LinearLayout sbLayout = (LinearLayout)
seekAdapter.getDropDownView(0, null, null);
SeekBar seekBar = (SeekBar) sbLayout.findViewById(R.id.v_seekbar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.d(TAG, "" + progress); // nope
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
与seekbar.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/highlight"
/>
<SeekBar
android:id="@+id/v_seekbar"
android:minWidth="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacer" />
</LinearLayout>
和 spinner_seek.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SEEKBAR"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/main" />
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/highlight" />
</LinearLayout>
它至少有一个下拉搜索栏,但我不知道如何获得对它的引用。
有人知道最后一公里怎么走吗?
编辑:我在写完之后注意到它PopupMenu
存在,但它只是 API 11+(我的项目是 10+)或 HoloEverywhere。希望在没有 ABS 之外的任何库的情况下做到这一点。