2017-05-09 68 views
1

我刚刚创建并包含工具栏的活动,我在工具栏中放置了一个勾号图标,但是当我运行应用程序时,勾号图标未出现在工具栏中,这是我的班级工具栏中的图标不出现

package mediaclub.app.paymob; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ImageView; 
import android.widget.ListView; 

import java.util.ArrayList; 

import mediaclub.app.paymob.Adapters.ContactsAdapter; 

public class AddMembersActivity extends Activity { 
    ArrayList<Contact> listContacts; 
    ListView lvContacts; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_members); 
     listContacts = new ContactFetcher(this).fetchAll(); 
     lvContacts = (ListView) findViewById(R.id.lvContacts); 
     ImageView checkContacts = (ImageView) findViewById(R.id.checkedId); 
     ContactsAdapter adapterContacts = new ContactsAdapter(this, listContacts); 
     lvContacts.setAdapter(adapterContacts); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(android.view.MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.checkedId: 
       Intent i = new Intent(AddMembersActivity.this, AddMembersActivity.class); 
       startActivity(i); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


} 

,这是菜单

<?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/checkedId" 
     android:icon="@drawable/checked" 
     android:title="@string/check" 
     app:showAsAction="always"/> 

</menu> 

这是XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:fab="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:background="#ffffff" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="mediaclub.app.paymob.AddMembersActivity"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:backgroundTint="#3cb5a4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="91" 
     android:orientation="horizontal" 
     android:weightSum="100" > 
     <ListView 
      android:id="@+id/lvContacts" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" > 
     </ListView> 
    </LinearLayout> 
</LinearLayout> 

很抱歉,如果在事情是不明确的,如果任何一个知道如何解决这个情况请你只回答我 对不起我的英文不好

+0

发表您的XML以及 –

+0

@quick学生请我编辑我的答案 – DevDev

回答

0

onCreate()添加此,初始化工具栏

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
0

你永远不会设置这就是工具栏问题尝试这种方法,并从称之为的onCreate

public void showToolBar() { 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("title for toolbar"); 

     setSupportActionBar(toolbar); 

     //toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow); 
     toolbar.setNavigationOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //handling the onclick 
        } 
       } 

     ); 
    } 
0

尽量延长​​从AppCompatActivity而不是Activity

public class AddMembersActivity extends AppCompatActivity { 

    ............. 
    .................. 
} 

2.onCreate()方法,初始化Toolbar小部件和使用方法setSupportActionBar()使用它作为一个ActionBar

public class AddMembersActivity extends AppCompatActivity { 
    ArrayList<Contact> listContacts; 
    ListView lvContacts; 

    Toolbar mToolBar; 

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

     mToolBar = (Toolbar) findViewById(R.id.toolbar); 
     if(mToolBar != null) { 
      setSupportActionBar(mToolBar); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     } 

     listContacts = new ContactFetcher(this).fetchAll(); 
     lvContacts = (ListView) findViewById(R.id.lvContacts); 
     ImageView checkContacts = (ImageView) findViewById(R.id.checkedId); 
     ContactsAdapter adapterContacts = new ContactsAdapter(this, listContacts); 
     lvContacts.setAdapter(adapterContacts); 
    } 

    ................. 
    ............................ 
} 

FYI,您必须添加下面的依赖在你build.gradle使用AppCompatActivity

dependencies { 
    .......... 
    .............. 
    compile 'com.android.support:appcompat-v7:25.1.0' 
} 

希望这将有助于〜