有一个过滤器图标,但我想动态添加从数据库中获取的项目,就像我点击项目一样,它将在此菜单中排序fliter icon 我希望这会很清楚 这是我的菜单xml代码
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_spinner"
android:actionViewClass="android.widget.Spinner"
android:visible="false"
android:showAsAction="always"/>
</menu>
这是我的MainActivity
package abtech.waiteriano.com.actionbartest;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private MenuItem mSpinnerItem = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
mSpinnerItem = menu.findItem( R.id.menu_spinner );
getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem );
return true;
}
private void setupSpinner( MenuItem item )
{
item.setVisible( getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST );
View view = item.getActionView();
if (view instanceof Spinner)
{
Spinner spinner = (Spinner) view;
spinner.setAdapter( ArrayAdapter.createFromResource( this,
R.array.spinner_data,
android.R.layout.simple_spinner_dropdown_item ) );
}
}
}
答案 0 :(得分:2)
您可以按如下方式获得所需的视图。
<强> activity_main.xml中强>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<android.support.v7.widget.Toolbar
android:id=”@+id/toolbar”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”?attr/colorPrimary”
android:minHeight=”?attr/actionBarSize” />
</LinearLayout>
创建一个spinner_dropdown_item.xml,其中包含TextView,它表示每个微调器项的视图
<强> spinner_dropdown_item.xml 强>
<?xml version=”1.0″ encoding=”utf-8″?>
<TextView xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@android:id/text1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@color/colorPrimary”
android:gravity=”center_vertical”
android:minHeight=”?android:attr/listPreferredItemHeightSmall”
android:paddingLeft=”12dp”
android:paddingRight=”12dp”
android:textAppearance=”?android:attr/textAppearanceListItemSmall” />
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private String[] category={"hello","this","is","me"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
category = getResources().getStringArray(R.array.category);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setLogo(R.drawable.ic_drawer);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
navigationSpinner.setAdapter(spinnerAdapter);
toolbar.addView(navigationSpinner, 0);
navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,
“you selected: ” + category[position],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
这是你的menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.na.quiz.activity.WelcomeActivity">
<item
android:id="@+id/action_logout"
android:orderInCategory="100"
android:title="@string/action_logout"
app:showAsAction="always"
android:icon="@android:drawable/ic_menu_search" />
<item
android:title="LIke"
android:orderInCategory="101"
android:id="@+id/like"
app:showAsAction="always"
android:icon="@drawable/com_facebook_button_like_icon_selected" />
</menu>
我希望它适合你。