简体   繁体   中英

Icons in Actionbar are not showing

I am using Action bar but the icons for some reason are not showing up

here is my activity xml code

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

here is the menu 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>

and the Mainfest file

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

I have tried some of the solutions posted by other like

android:showAsAction ="always"

its still not working then i tried to change the theme of the app still same thing

Thanks in advance

Here is the Activity Java File

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

Here is A Screenshot of the Activity

活动截图1

活动截图2

Can you try this:

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

try this

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

Replace your menu_main.xml with the following code :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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