在我的应用程序中,我使用导航抽屉创建了侧面菜单。我需要根据点击状态更改菜单列表项目背景颜色。所以我使用<selector>
来实现此目的。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" ><color android:color="@color/red"/></item>
<item android:state_focused="true"><color android:color="@color/yellow"/></item>
<item android:state_activated="true"><color android:color="@color/green"/></item>
<item><color android:color="#ddd"/></item>
</selector>
以上代码工作正常。单击导航抽屉背景中的项目背景颜色基于上述选择器正在变化,导航抽屉也将关闭。
但我的问题是如果我打开导航抽屉菜单项背景颜色变为默认值。所以我需要为菜单项设置相同的背景颜色。
然后,如果我打开导航抽屉,我的菜单项将变为默认颜色。没有项目的背景颜色。
但我需要根据当前活动设置菜单项的背景颜色。
仅当导航抽屉关闭时,背景颜色才会消失。
请帮帮我。
抱歉我的英语很差。
提前致谢。
package com.example.qh_test;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TechPageActivity extends ActionBarActivity implements
OnItemClickListener {
private DrawerLayout drawerlayout;
private ListView listview;
private String[] menus;
private ActionBarDrawerToggle drawerListener;
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tech_page);
drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
drawerListener = new ActionBarDrawerToggle(this, drawerlayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
}
};
drawerlayout.setDrawerListener(drawerListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
menus = getResources().getStringArray(R.array.tech_menus);
listview = (ListView) findViewById(R.id.drawerList);
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, menus));
listview.setOnItemClickListener(this);
webview = (WebView) findViewById(R.id.homeWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
webview.loadUrl("http://stackoverflow.com/");
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tech_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
selectItem(position);
//SelectItem.get(position).setColor(getResources().getColor("your color"));
switch (position) {
case 0:
webview.loadUrl("http://stackoverflow.com/");
listview.setItemChecked(position, true);
drawerlayout.closeDrawer(listview);
break;
case 1:
webview.loadUrl("http://stackoverflow.com/questions");
listview.setItemChecked(position, true);
drawerlayout.closeDrawers();
break;
case 2:
webview.loadUrl("http://stackoverflow.com/unanswered");
listview.setItemChecked(position, true);
drawerlayout.closeDrawers();
break;
}
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listview.setItemChecked(position, true);
setTitle(menus[position]);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
答案 0 :(得分:0)
检查此示例自定义导航抽屉
在onclick里面
SelectItem.get(position).setColor(getResources().getColor("your color"));
http://www.tutecentral.com/android-custom-navigation-drawer/
答案 1 :(得分:0)
你可以在你的活动的onStart方法中调用这样的方法(使用ListView mDrawerList和你的DrawerAdapter作为mDrawerAdapter):
protected void selectItem(int position)
{
if (mDrawerList != null) {
mDrawerList.setItemChecked(position, true);
mDrawerAdapter.setSelected(position);
}
}
答案 2 :(得分:0)
在OnClickListener for NavigationDrawer菜单调用方法setItemChecked(int position,true)