我有这样的代码:
抽屉布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/drawerList"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:entries="@array/Functions" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
抽屉活动:
public class Drawer extends Activity {
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle mDrawerToggle;
private Intent intent;
public RelativeLayout fullLayout;
public FrameLayout frameLayout;
@Override
public void setContentView(int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.content_frame);
drawerLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawerLayout);
drawerList = (ListView) fullLayout.findViewById(R.id.drawerList);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
drawerList.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.Functions)));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {}
public void onDrawerOpened(View drawerView){}
};
drawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setIcon(R.drawable.ic_drawer);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void selectItem(int position) {
drawerLayout.closeDrawers();
switch (position) {
case 0:
intent = new Intent(this, 0Act.class);
startActivity(intent);
break;
case 1:
// intent = new Intent(this, 1Act.class);
break;
case 2:
intent = new Intent(this, 2Act.class);
startActivity(intent);
break;
case 3:
intent = new Intent(this, 3Act.class);
startActivity(intent);
break;
case 4:
// intent = new Intent(this, 4Act.class);
break;
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
}
第一活动:
public class 0Act extends Drawer implements ActionBar.TabListener {
ActionBar.Tab t1,t2,t3;
ActionBar actionBar;
Button oneButton;
final CharSequence[] items = {"1", "2", "3", "4"};
AlertDialog.Builder ad;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.0Act);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
t1 = actionBar.newTab().setText("1");
t2 = actionBar.newTab().setText("2");
t3 = actionBar.newTab().setText("3");
t1.setTabListener(this);
t2.setTabListener(this);
t3.setTabListener(this);
actionBar.addTab(t1);
actionBar.addTab(t2);
actionBar.addTab(t3);
actionBar.setSelectedNavigationItem(0);
oneButton= (Button) this.findViewById(R.id.oneButton);
oneButton.setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v) {ad.show();}});
ad = new AlertDialog.Builder(getActivity());
ad.setTitle("Menu");
ad.setItems(items, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {}});
}
}
我看到抽屉 - 我可以单击抽屉,但我无法单击第一个活动中的任何按钮。出了什么问题?在将抽屉添加到许多活动之前,所有活动都运行良好,但现在我没有看到任何错误,活动不起作用。
提及 super.onClick(v); 在 onClick 方法中为 Item1Activity 类