Android ActionBar图标丢失

时间:2015-07-27 11:48:16

标签: android android-actionbar android-actionbaractivity

我想在ActionBar中添加一个图标,但是我无法让ActionBar显示我在菜单文件中定义的图标。我已经尝试将Activity更改为ActionBarActivity但是,如果我这样做,应用程序崩溃了。我也尝试将主题更改为Theme.AppCompat,但这不起作用。这是我的代码:

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);
}


}

在我的清单中我有这个:          

<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>

以下是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>

这是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>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

步骤1:在菜单资源中将所有app:替换为android:app:appcompat-v7或其他库一起使用,您使用的是原生操作栏。

步骤2:如果您希望action_settings在操作栏中显示为工具栏按钮,请将android:showAsAction="never"替换为android:showAsAction="ifRoom"(或者always而不是{{} 1}})。