更改导航抽屉中所选项目的背景颜色

时间:2014-04-30 19:25:07

标签: java android android-layout navigation-drawer

我想更改导航抽屉中所选项目的蓝色。我可以在点击某个项目但不在活动项目中时更改它。这就是我使用的:在drawable文件夹中

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_activated="true" android:drawable="@color/default_color" />
    <item android:state_selected="true" android:drawable="@color/uva_color" />
    <item android:state_pressed="true" android:drawable="@color/uva_color" />
    <item android:state_focused="true" android:drawable="@color/default_color" />
    <item android:drawable="@color/default_color" />  
</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="uva_color">#f2f8ee</color>
    <color name="default_color">#f2f8ee</color>
</resources>

然后在导航抽屉的布局中:

<ListView
        android:id="@+id/left_drawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:listSelector="@drawable/activated_background"
        android:background="#ffff"/>

因此,选择的当前项目不会改变颜色。任何想法?

使用java nav抽屉编辑:

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;

List<DrawerItem> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/** Customizzo la actionbar */
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbarcustom);
        //actionBar.setDisplayShowTitleEnabled(false);
        //actionBar.setDisplayShowCustomEnabled(true);
setTitle("FTV");
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
    TextView title = (TextView) findViewById(actionBarTitleId);
    if (title != null) {
        title.setTextColor(Color.WHITE);
    }
}

/**
* Controllo la versione di android, se Kitkat o superiore rendo la nav bar trasparente
* */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's onCreate() for instance

        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

SystemBarTintManager systemBarTintManager = new SystemBarTintManager(this);
systemBarTintManager.setStatusBarTintEnabled(true);
systemBarTintManager.setStatusBarTintColor(Color.parseColor("#ff3600"));

/*systemBarTintManager.setNavigationBarTintEnabled(true);
systemBarTintManager.setNavigationBarTintColor(Color.parseColor("#CC0000"));*/

    }

// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);


mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);

// Add Drawer Item to dataList
dataList.add(new DrawerItem(true)); // adding a spinner to the list

dataList.add(new DrawerItem("Preferiti")); // adding a header to the list
dataList.add(new DrawerItem("Avvisi", R.drawable.avvisi));
dataList.add(new DrawerItem("Pianifica viaggio", R.drawable.bus));
dataList.add(new DrawerItem("Mappe e Tratte", R.drawable.maps));
//dataList.add(new DrawerItem("Lables", R.drawable.ic_action_labels));

dataList.add(new DrawerItem("Impostazioni"));// adding a header to the list
dataList.add(new DrawerItem("Settings", R.drawable.settings));
/*dataList.add(new DrawerItem("Cloud", R.drawable.ic_action_cloud));
dataList.add(new DrawerItem("Camara", R.drawable.ic_action_camera));
dataList.add(new DrawerItem("Video", R.drawable.ic_action_video));
dataList.add(new DrawerItem("Groups", R.drawable.ic_action_group));
dataList.add(new DrawerItem("Import & Export",
R.drawable.ic_action_import_export));*/

dataList.add(new DrawerItem("Altro")); // adding a header to the list
dataList.add(new DrawerItem("About", R.drawable.about));
//dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
dataList.add(new DrawerItem("Help", R.drawable.help));

adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);

mDrawerList.setAdapter(adapter);

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {

if (dataList.get(0).isSpinner()
& dataList.get(1).getTitle() != null) {
SelectItem(2);
} else if (dataList.get(0).getTitle() != null) {
SelectItem(1);
} else {
SelectItem(0);
}
}

}

@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;
}

public void SelectItem(int possition) {

Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {


case 2:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());

break;
case 3:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 4:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 5:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 6:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 7:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 8:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 9:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 10:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 11:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 12:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;  
case 13:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 14:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
default:
break;
}

fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();

mDrawerList.setItemChecked(possition, true);
//mDrawerList.getChildAt(possition).setBackgroundResource(R.drawable.activated_background);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}

@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);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return false;
}

private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (dataList.get(position).getTitle() == null) {
SelectItem(position);
}

}
}

}

这是激活项目中的标准颜色,我不想要!

enter image description here

我想要这个:#f2f8ee

3 个答案:

答案 0 :(得分:2)

看起来很好......

在托管listview的java代码中,调用myListView.setItemChecked(position, true);

此外,您为state_activated设置的颜色与抽屉处于normal状态时的颜色相同。这是打算吗?...

使用TextView创建一个xml文件

nav_drawer_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android">
    android:id="@+id/textId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/[your_selector]" />
{/ 1}}中的

适用于您的适配器

getview()

答案 1 :(得分:2)

检查此示例自定义导航抽屉

在onclick里面

  SelectItem.get(position).setColor(getResources().getColor("your color")); 

答案 2 :(得分:0)

这是我的工作方式,它正在运作,简短的概念维持适配器中所选项目的位置并调用{ {1}}在调用notifyDataSetChanged时再次调用notifyDatasetChanged方法,并在获取视图中检查所选位置上的位置是否更改背景视图。有关详细信息,请参阅this answer。

如果您有任何疑惑,请随时提出。