我是java和android的初学者。我想添加一个抽屉来繁殖活动。我创建了一个基础活动。一切都有效,除非我点击动作栏上的抽屉图标没有显示菜单。怎么了!?
DrawerActivity.java
package com.myapp.app;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AdapterView;
public class DrawerActivity extends Activity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout fullLayout;
private FrameLayout frameLayout;
FrameLayout progressBarHolder;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
@Override
public void setContentView(int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
//Your drawer content...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_drawer);
mPlanetTitles = new String[]{getString(R.string.menuAdsGold), getString(R.string.menuAds), getString(R.string.menuSearch), getString(R.string.menuAcc), getString(R.string.menuAbout) };
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
//getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
drawerView.bringToFront();
//getActionBar().setTitle(mTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setTitle(getString(R.string.app_name));
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Pass the event to ActionBarDrawerToggle, if it returns
//true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
//Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();
//Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
//setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
switch (position) {
case 0:
break;
case 1:
break;
/*
case 2:
Intent i = new Intent(this,SearchActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivity(i);
break;
case 3:
Intent i = new Intent(this,AccActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivity(i);
break;
case 4:
Intent i = new Intent(this,AboutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivity(i);
break;
*/
default:
break;
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
public void showProgress(Boolean show) {
// prepare for a progress bar dialog
if(show) {
progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);
progressBarHolder.setVisibility(View.VISIBLE);
progressBarHolder.bringToFront();
} else {
progressBarHolder.setVisibility(View.GONE);
}
}
public void showProgress(Boolean show,int sec) {
// prepare for a progress bar dialog
if(show) {
progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);
progressBarHolder.setVisibility(View.VISIBLE);
progressBarHolder.bringToFront();
} else {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressBarHolder.setVisibility(View.GONE);
}
}, sec);
}
}
}
MainActivity.java
package com.myapp.app;
import java.util.Arrays;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import com.amlaksara.app.Estate;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class MainActivity extends DrawerActivity {
private TextView htext;
private ListView listView1;
AsyncHttpClient client =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads);
// super.onCreateDrawer();
try{
String ex = getIntent().getExtras().getString("bGold");
if(ex.equals("false"))
bGold=false;
else
bGold=true;
} catch(Exception e) {
}
getActionBar().setTitle(getString(R.string.app_name));
// reload_stream();
// listView1 = (ListView)findViewById(R.id.listView1);
// View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
// listView1.addHeaderView(header);
// View footer = (View)getLayoutInflater().inflate(R.layout.listview_footer_row, null);
// listView1.addFooterView(footer);
}
}
activity_drawer.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:context=".AdsGoldActivity" >
<FrameLayout
android:id="@+id/progressBarHolder"
android:animateLayoutChanges="true"
android:visibility="gone"
android:alpha="0.4"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center" />
</FrameLayout>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml中
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:context=".AdsGoldActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
更新 我发现当我将DrawerActivity更改为启动活动时,它可以工作,但是当我将MainActivity设置为启动时,图标无效。
答案 0 :(得分:2)
可以使用getSupportActionBar()
代替getActionBar()
方法。并使用DrawerActivity
代替ActionBarActivity
扩展您的Activity
。
答案 1 :(得分:1)
将您的onOptionsItemSelected
更改为:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
答案 2 :(得分:0)
我通过在DrawerActivity中更改onCreate的名称并从MainAcitity的onCreate事件调用它来修复它!