我在我的应用程序中使用android动作栏我已经成功实现了动作栏,但它仅适用于3.0或更高版本
文档说动作栏是在android蜂窝中引入的,它有api版本11,我的代码工作时我有android:minSdkVersion 11,我想让它在Android 2.2或更高版本上工作 即android:minSdkVersion =" 8"
我已经包含了 import android.support.v7.app.ActionBar ;然而,即使这样它也无法正常工作
我不想使用actionbarsherlock
我想使用getActionBar()。setDisplayHomeAsUpEnabled(true),
我甚至尝试过使用getSupportActionBar()。setDisplayHomeAsUpEnabled(true);它也不起作用这是我的代码给我错误消息方法getSupportActionBar()未定义类型活动
package com.Blog;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import com.Blog.image.ImageLoader;
import com.Blog.parser.RSSFeed;
public class ListActivity extends Activity implements OnItemClickListener {
RSSFeed feed;
ListView lv;
CustomListAdapter adapter;
String feedLink;
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
private ActionBarDrawerToggle drawerListener;
//private ActionBarDrawerToggle drawerListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feed_list);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer,R.string.drawer_open, R.string.drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
//super.onDrawerClosed(drawerView);
//Toast.makeText(ListActivity.this, "Drawer closed", Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
//super.onDrawerOpened(drawerView);
//Toast.makeText(ListActivity.this, "Drawer opened", Toast.LENGTH_LONG).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
//getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
listView = (ListView) findViewById(R.id.drawerlist);
planets = getResources().getStringArray(R.array.planets);
listView.setAdapter(new ArrayAdapter<>(ListActivity.this, android.R.layout.simple_list_item_1, planets ));
listView.setOnItemClickListener(this);
// set the feed link for refresh
//feedLink = new SplashActivity().RSSFEEDURL;
//feedLink = new SplashActivity().RSSFEEDURL;
// Get feed form the file
feed = (RSSFeed) getIntent().getExtras().get("feed");
// Initialize the variables:
lv = (ListView) findViewById(R.id.listView);
lv.setVerticalFadingEdgeEnabled(true);
// Set an Adapter to the ListView
adapter = new CustomListAdapter(this);
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(ListActivity.this,
DetailActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}
Intent intent = null;
switch (item.getItemId()) {
case R.id.action_rate:
String webpage = "http://developer.android.com/index.html";
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent);
break;
case R.id.action_share:
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello!");
intent.setType("text/plain");
startActivity(intent);
break;
case R.id.menuSortNewest:
Intent intent1 = new Intent(this, aSearch.class);
startActivity(intent1);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
break;
case R.id.menuSortRating:
Intent intent2 = new Intent(this, CSearch.class);
startActivity(intent2);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
// List adapter class
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter(ListActivity activity) {
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
// Set the total list item count
return feed.getItemCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.date);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//Toast.makeText(ListActivity.this, "Drawer closed", Toast.LENGTH_LONG).show();
if (position == 0) {
Intent intent = new Intent(this, bListActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, aListActivity.class);
startActivity(intent);
}
else if (position == 2) {
Intent intent = new Intent(this, ccategoryListActivity.class);
startActivity(intent);
}
else if (position == 3) {
Intent intent = new Intent(this, dListActivity.class);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(this, eActivity.class);
startActivity(intent);
}
else if (position == 5) {
Intent intent = new Intent(this, SplashActivityQ.class);
intent.putExtra("country", "Exam");
startActivity(intent);
}
else if (position == 6) {
Intent intent = new Intent(this, SplashActivityQ1.class);
intent.putExtra("country", "Quiz");
startActivity(intent);
}
else if (position == 7) {
String webpage = "http://developer.android.com/index.html";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent);
}
else if (position == 8) {
String webpage = "http://developer.android.com/index.html";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent);
}
else if (position == 9) {
String webpage = "http://developer.android.com/index.html";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent);
}
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
}
android mainfest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<activity
android:name="com.Blog.ListActivity"
android:configChanges="orientation"
android:theme="@style/MyTheme" />
styles.xml
</style>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>