我对此进行了编码,以便在OptionsMenu上获得一个Filter图标。我使用的是带有可绘制图像的空白框,其onClick显示了DialogFragment。但这不能正常工作,因为在OptionsMenu上出现一个空标题项目,单击此对话框打开了我的DialogFragment。
感谢您的帮助。
谢谢。
OptionsMenu的菜单文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/filter"
android:orderInCategory="100"
android:icon="@drawable/filter_icon"
android:showAsAction="ifRoom|withText"
android:title="hi" />
</menu>
这是我的活动:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
String[] values = new String[]{ "Veg.",
"Non Veg.",
"Veg & Non veg."
};
int id = item.getItemId();
if (id == R.id.filter) {
RestaurantListingFragment restaurant_categories_dialog = new RestaurantListingFragment();
android.app.FragmentManager fm = getFragmentManager();
Bundle args = new Bundle();
args.putStringArray("restaurantCategories",values);
restaurant_categories_dialog.setArguments(args);
// Show DialogFragment
restaurant_categories_dialog.show(fm, "hi");
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的片段类:
public class RestaurantListingFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog_restaurant_categories, container,false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
String[] restaurantCategories = getArguments().getStringArray("restaurantCategories");
if(restaurantCategories != null)
{
ListView lv=(ListView) rootView.findViewById(R.id.fd_lv);
ArrayAdapter arrayAdapter =new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,restaurantCategories);
lv.setAdapter(arrayAdapter);
}
return rootView;
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cresol.demo.drestodemo">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true">
<activity
android:name=".Home"
android:theme="@style/NoTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".signup"
android:theme="@style/NoTitle" />
<activity
android:name=".RestaurantListing"
android:theme="@style/AppTheme" />
<activity android:name=".DishListing"
android:theme="@style/AppTheme"></activity>
</application>
</manifest>
最佳答案
在菜单文件中更改此行android:showAsAction="ifRoom|withText"
与这个app:showAsAction="always"