ActionBar中不显示图标

时间:2014-11-07 09:48:27

标签: android android-actionbar android-support-library android-sharing

感谢你,我解决了很多问题。 在我的应用程序中,我已将ActionBar激活为android-support-v7-compat。你可以很好地看到ActionBar但是在ActionBar上放了一个共享图标,只显示了空白ActioBar。没有图标显示共享。

活动代码如下:

public class mainActivity extends ActionBarActivity {the
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private Fragment[] fragments = new Fragment[]{
                                                new asdfFragment(),
                                                new asdfghFragment(),
                                                new 2135Fragment(),
                                                new 548uhFragment(),
                                                new 007897Fragment(),
                                                new wehthbgFragment(),
                                                new werbhnrFragment(),
                                             };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    drawerList = (ListView) findViewById(R.id.leftDrawer);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerOptions = getResources().getStringArray(R.array.drawer_options);

    drawerList.setAdapter(new ArrayAdapter<String> (this,
                                                   R.layout.drawer_list_item,
                                                   drawerOptions));

    drawerList.setItemChecked(0, true);
    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
           .add(R.id.contentFrame, fragments[0])
           .add(R.id.contentFrame, fragments[1])
           .add(R.id.contentFrame, fragments[2])
           .add(R.id.contentFrame, fragments[3])
           .add(R.id.contentFrame, fragments[4])
           .add(R.id.contentFrame, fragments[5])
           .add(R.id.contentFrame, fragments[6])
           .hide(fragments[1])
           .hide(fragments[2])
           .hide(fragments[3])
           .hide(fragments[4])
           .hide(fragments[5])
           .hide(fragments[6])
           .commit();

    ActionBar actionbar = getSupportActionBar();


}

public void setContent(int index){

    Fragment toHide1 = null;
    Fragment toHide2 = null;
    Fragment toHide3 = null;
    Fragment toHide4 = null;
    Fragment toHide5 = null;
    Fragment toHide6 = null;
    Fragment toShow = null;
    switch (index){
        case 0:
            toHide1 = fragments[1];
            toHide2 = fragments[2];
            toHide3 = fragments[3];
            toHide4 = fragments[4];
            toHide5 = fragments[5];
            toHide6 = fragments[6];
            toShow = fragments[0];
            break;
        case 1:
            toHide1 = fragments[0];
            toHide2 = fragments[2];
            toHide3 = fragments[3];
            toHide4 = fragments[4];
            toHide5 = fragments[5];
            toHide6 = fragments[6];
            toShow = fragments[1];
            break;
        case 2:
            toHide1 = fragments[1];
            toHide2 = fragments[0];
            toHide3 = fragments[3];
            toHide4 = fragments[4];
            toHide5 = fragments[5];
            toHide6 = fragments[6];
            toShow = fragments[2];
            break;
        case 3:
            toHide1 = fragments[1];
            toHide2 = fragments[2];
            toHide3 = fragments[0];
            toHide4 = fragments[4];
            toHide5 = fragments[5];
            toHide6 = fragments[6];
            toShow = fragments[3];
            break;
        case 4:
            toHide1 = fragments[1];
            toHide2 = fragments[2];
            toHide3 = fragments[3];
            toHide4 = fragments[0];
            toHide5 = fragments[5];
            toHide6 = fragments[6];
            toShow = fragments[4];
            break;
        case 5:
            toHide1 = fragments[1];
            toHide2 = fragments[2];
            toHide3 = fragments[3];
            toHide4 = fragments[4];
            toHide5 = fragments[0];
            toHide6 = fragments[6];
            toShow = fragments[5];
            break;
        case 6:
            toHide1 = fragments[1];
            toHide2 = fragments[2];
            toHide3 = fragments[3];
            toHide4 = fragments[4];
            toHide5 = fragments[5];
            toHide6 = fragments[0];
            toShow = fragments[6];
            break;
    }

    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
           .hide(toHide1)
           .hide(toHide2)
           .hide(toHide3)
           .hide(toHide4)
           .hide(toHide5)
           .hide(toHide6)
           .show(toShow)
           .commit();

    drawerList.setItemChecked(index, true);
    drawerLayout.closeDrawer(drawerList);
}


class DrawerItemClickListener implements ListView.OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        setContent(position);

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actionbar_menu, menu);
    return true;

}

}

菜单代码如下:

<?xml version="1.0" encoding="utf-8"?>

<item 
    android:id="@+id/action_share"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_share"
    android:title="@string/action_share"/>

清单代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="7" 
android:versionName="1.0.14"
package="my.app" 
android:installLocation="auto">

<uses-sdk android:minSdkVersion="7"
          android:targetSdkVersion="7"/>

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

    <activity android:label="@string/app_name"
        android:name="my.app.Activity.mainActivity" 
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

非常感谢所有人

1 个答案:

答案 0 :(得分:2)

将menu.xml更改为:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item 
    android:id="@+id/action_share"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_share"
    android:title="@string/action_share"/>
</menu>

请注意我使用app:showAsAction而不是android:showAsAction