Ich habe dies codiert, um ein Filtersymbol bei OptionsMenu zu erhalten. Ich benutzte eine leere meny mit einem drawbaren Bild und der onClick von diesem zeigte ein DialogFragment. Aber das funktioniert nicht gut, da ein leeres Titelelement auf dem OptionsMenu angezeigt wird, auf den mein DialogFragment geöffnet wird. Ich freue mich über jede Hilfe. Danke.Wie ändert man das Standard-Icon von OptionsMenu in Android?
Menü Datei für Optionsmenü:
<?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>
Dies ist MyActivity:
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);
}
Das ist mein Fragment Klasse:
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;
}
}
Manifests:
<?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>
diese Zeile ändern. In Ihrer Änderung Menü Datei diese Zeile 'android: showAsAction = "ifRoom | withText"' mit diesem 'App: showAsAction = "always"' –
Ich habe gerade versucht, dass .. aber es funktioniert nicht. Ich schätze Ihre Hilfe. –
Sind Sie sicher, Sie verwenden 'app: showAsAction =" immer "' und nicht 'android: showAsAction =" immer "' –