为了在OptionsMenu上获得一个Filter图标,我编码了这个。我正在使用带有可绘制图像的空meny,而onClick就显示了DialogFragment。但是这不能正常工作,因为在我的DialogFragment打开的OptionsMenu上会出现一个空标题项。 我感谢任何帮助。 感谢。
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>
答案 0 :(得分:2)
在您的菜单文件中更改此行
android:showAsAction="ifRoom|withText"
用这个
app:showAsAction="always"