我为操作栏设置了自定义主题,以便在屏幕顶部显示三个按钮。点击最左边的按钮,我想开始一个新的活动。但是,我无法这样做。我使用了正确的方法来启动活动,但我仍然遇到错误。我不知道问题是什么。
到目前为止我写的代码。
MainActivity.java
package com.example.contactmanager1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(R.layout.button_layout);
getActionBar().setDisplayShowHomeEnabled(false);
listView = (ListView)findViewById(R.id.main_contact_listview);
button1= (ImageButton)findViewById(R.id.button_search);
button2= (ImageButton)findViewById(R.id.button_addcontact);
button3= (ImageButton)findViewById(R.id.button_options);
setUpListView();
}
private void setUpListView(){
List <String> displayList = new ArrayList<String>();
displayList.add("Display Item 1");
displayList.add("Display Item 2");
displayList.add("Display Item 3");
ListAdapter listAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,displayList);
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
//Handle presses on the action bar items
switch(item.getItemId()){
case R.id.action_button_groups:
Intent intent = new Intent(this,Groups.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void addContact(View view){
Intent intent = new Intent(this,AddContact.class);
startActivity(intent);
}
public void groupPage(View view){
Intent intent = new Intent(this,Groups.class);
startActivity(intent);
}
}
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:id="@+id/action_button_groups"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/groups"
android:onClick="groupPage" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contactlist" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/favourites" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
对于button_layout.xml中的第一个图像按钮,我添加了一个指向方法groupPage的onClick属性。我在MainActivity.java中定义了这个方法。