Actionbar中的图标未显示

时间:2015-12-29 08:29:44

标签: java android xml android-layout

我正在使用操作栏但由于某些原因图标没有显示

这是我的活动xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.menz.projectv4.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LVTimetable"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:choiceMode="none" />
</RelativeLayout>

这里是菜单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="com.example.menz.projectv4.MainActivity">

    <item android:id="@+id/action_addReminder"
        android:title="Add Reminder"
        android:icon="@drawable/add"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/action_search"
        android:title="Search"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom"/>


</menu>

和Mainfest文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.menz.projectv4">

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.DeviceDefault.Light.DarkActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".add" />
        <activity android:name=".display" />
        <activity android:name=".search"></activity>
    </application>

</manifest>

我尝试了其他类似

发布的一些解决方案
android:showAsAction ="always"

它还没有工作然后我试图改变应用程序的主题仍然是相同的事情

提前致谢

这是活动Java文件

package com.example.menz.projectv4;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity implements  ListView.OnItemClickListener{
    static String row_id = "row_id";
    private ListView Timetable ;
    private CursorAdapter cursorAdapter;
    private DatabaseConnector databaseConnector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        Timetable = (ListView)findViewById(R.id.LVTimetable);
        Timetable.setOnItemClickListener(this);

        String [] from = new String [] {"Subject","Assignment"};
        int [] to = new int [] {R.id.TVTitle,R.id.TVSubject};
        cursorAdapter = new SimpleCursorAdapter(MainActivity.this,R.layout.list_view_layout,null,from,to);
        Timetable.setAdapter(cursorAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();

        new   GetAllTask().execute((Object[]) null);
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent viewTask = new Intent(this,display.class);
        viewTask.putExtra(row_id,id);
        startActivity(viewTask);

    }

    private class GetAllTask extends AsyncTask<Object , Object , Cursor>
    {
        DatabaseConnector databaseConnector = new DatabaseConnector(MainActivity.this);

        @Override
        protected Cursor doInBackground(Object... params) {
            try {
                databaseConnector.open();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            return databaseConnector.getAllTasks();
        }

        @Override
        protected void onPostExecute(Cursor result) {
            cursorAdapter.changeCursor(result);
            databaseConnector.close();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_addReminder) {
            startActivity(new Intent(this,add.class));
        }

        return super.onOptionsItemSelected(item);
    }

这是活动的屏幕截图

Activity Screenshot 1

Activity Screenshot 2

3 个答案:

答案 0 :(得分:0)

你可以试试这个:

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.example.menz.projectv4.MainActivity">

    <item android:id="@+id/action_addReminder"
        android:title="Add Reminder"
        android:icon="@drawable/add"
        android:showAsAction="always"/>
    <item android:id="@+id/action_search"
        android:title="Search"
        android:icon="@drawable/search"
        android:showAsAction="always"/>


</menu>

答案 1 :(得分:0)

试试这个

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/save_btn"
        android:orderInCategory="100"
        android:title="Save"
        android:icon="@drawable/icon_save"
        app:showAsAction="always"/>

答案 2 :(得分:0)

使用以下代码替换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="com.example.menz.projectv4.MainActivity" >

<item
    android:id="@+id/action_chat"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:icon="@drawable/add"
    app:showAsAction="always"/>
<item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:icon="@drawable/search"
    app:showAsAction="always"/>