简体   繁体   English

Android ActionBar图标丢失

[英]Android ActionBar icons are missing

I would like to add an icon to my ActionBar but I can't get the ActionBar to show the icons I defined inside the menu file. 我想向我的ActionBar添加一个图标,但是我无法让ActionBar显示我在菜单文件中定义的图标。 I've tried changing Activity to ActionBarActivity but, if I do that, the app crashes. 我尝试将Activity更改为ActionBarActivity,但如果这样做,该应用程序将崩溃。 Also I tried changing the theme to Theme.AppCompat, but that didn't work. 我也尝试将主题更改为Theme.AppCompat,但这没有用。 Here is my code: 这是我的代码:

public class MainActivity extends Activity {


private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;

private CharSequence activityTitle;
private CharSequence itemTitle;
private String[] tagTitles;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    itemTitle = activityTitle = getTitle();
    tagTitles = getResources().getStringArray(R.array.nav_options);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ListView) findViewById(R.id.left_drawer);

    View header = getLayoutInflater().inflate(R.layout.drawer_cabecera, null);
    drawerList.addHeaderView(header);


    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    ArrayList<item_object> items = new ArrayList<item_object>();
    items.add(new item_object(tagTitles[1],R.drawable.ic_action_person));
    items.add(new item_object(tagTitles[2],R.drawable.ic_action_search));
    items.add(new item_object(tagTitles[3],R.drawable.ic_action_sd_storage));
    items.add(new item_object(tagTitles[4],R.drawable.ic_action_cloud));
    items.add(new item_object(tagTitles[5],R.drawable.ic_action_about));


    drawerList.setAdapter(new NavigationAdapter(this, items));
    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(
            this,
            drawerLayout,
            //R.drawable.ic_drawer,
            R.string.drawer_open,
            R.string.drawer_close
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(itemTitle);

        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(activityTitle);


        }
    };

    drawerLayout.setDrawerListener(drawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}



private class DrawerItemClickListener implements ListView.OnItemClickListener     {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    Fragment fragment = null;
    Class clase = null;
    Activity actividad = null;


    switch (position) {

        case 1:
            fragment = new PerfilFragment();
            break;
        case 2:
            fragment = new SeleccionarFragment();
            break;
        case 3:
            fragment = new GestorFragment();
            break;
        case 4:
            fragment = new DriveFragment();
            break;
        case 5:
            fragment = new InfoFragment();
            break;
        default:
            break;

    }

    if (fragment != null) {

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,     fragment).commit();

        drawerList.setItemChecked(position, true);
        drawerList.setSelection(position);
        setTitle(tagTitles[position]);
        drawerLayout.closeDrawer(drawerList);
    } else {
        // Error al crear el fragmento
        Log.e("MainActivity", "Error al crear el fragmento");

    }
}

@Override
public void setTitle(CharSequence title) {
    itemTitle = title;
    getActionBar().setTitle(itemTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sincronizar el estado del drawer
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}


}

In my manifest I have this: 在我的清单中,我有:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<uses-feature
    android:name="android.hardware.wifi"
    android:required="true" >
</uses-feature>

<application
    android:allowBackup="true"
    android:icon="@drawable/logo1"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar">

    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MainActivity"></activity>
    <activity android:name=".GestorActivity"></activity>

    <activity
        android:name="etsiit.etsiitcast_def.SeleccionarActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>



</application>

Here is the layout for MainActivity 这是MainActivity的布局

<android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"/>


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#CFCFCF"
    android:dividerHeight="2dp"
    android:background="#424242"
    android:textColor="#424242"
    />

</android.support.v4.widget.DrawerLayout>

And here is the menu_main.xml code: 这是menu_main.xml代码:

<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=".MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />

<item
    android:id="@+id/action_search"
    android:orderInCategory="200"
    android:title="Search"
    android:icon="@drawable/cast_icon_blanco"
    app:showAsAction="ifRoom"
    ></item>

</menu>

Thank you for your help. 谢谢您的帮助。

Step #1: Replace all occurrences of app: with android: in your menu resource. 步骤1:在菜单资源中将所有出现的app: :替换为android: app: is used with appcompat-v7 or other libraries, and you are using the native action bar. app:appcompat-v7或其他库一起使用,并且您正在使用本机操作栏。

Step #2: If you want action_settings to appear as a toolbar button in the action bar, replace android:showAsAction="never" with android:showAsAction="ifRoom" (or perhaps always instead of ifRoom ). 步骤#2:如果您希望action_settings在操作栏中显示为工具栏按钮,则将android:showAsAction="never"替换为android:showAsAction="ifRoom" (或者always替换为ifRoom )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM