这个问题在SO上已经被问了很多,我已经引用了所有的答案.我的导航抽屉上的所选项目仍然保留默认的Holo蓝色背景.我是Java新手,我对"上下文"部分感到困惑.setAdapter()
.
我的项目是一个Activity,使用导航抽屉交换了多个片段.
这是我的适配器:
mDrawerListView.setAdapter(new ArrayAdapter<String>(
// First parameter - Context
getActionBar().getThemedContext(),
// Second parameter - Layout for the row
R.layout.fragment_navigation_drawer_list_item,
// Third parameter - ID of the TextView to which the data is written
android.R.id.text1,
// Forth - the Array of data
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
Run Code Online (Sandbox Code Playgroud)
这里的上下文来自Android Studio中的"预先烹饪"导航抽屉.我认为这将是所选项目的导航抽屉项目背景颜色的答案.所以我改变了我的上下文getActivity().getBaseContext(),
,但这并没有改变任何东西.
我的主题(styles.xml
):
<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/ActionBar</item>
</style>
<!-- Navigation …
Run Code Online (Sandbox Code Playgroud) 嗨,谢谢你的阅读.
我有一个问题,我的Android应用程序导航抽屉,我无法改变蓝色的颜色 - 我已经过了所有其他问题从SO,参考Android文档,并尝试了一切到目前为止...但仍然没有运气.我真的希望有人可以提供帮助.
到目前为止的代码:
my_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Blue still showing up! -->
<item android:state_activated="true" android:drawable="@color/lightPink" />
<item android:state_selected="true" android:drawable="@color/lightPink" />
<item android:state_pressed="true" android:drawable="@color/lightPink" />
<item android:state_focused="true" android:drawable="@color/lightPink" />
<item android:drawable="@color/lightPink" />
</selector>
Run Code Online (Sandbox Code Playgroud)
styles.xml
<item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
Run Code Online (Sandbox Code Playgroud)
fragment_navigation_drawer.xml
<ListView 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:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:listSelector="@drawable/my_background"
tools:context="com.randompractice.app.NavigationDrawerFragment"
/>
Run Code Online (Sandbox Code Playgroud)
我现在已经被困在这24小时了,这让我发疯了.对于我的好奇心告诉我实施的这种愚蠢的小变化,已经变成了一个研究项目.谁能看到我做错了什么?
我想更改导航抽屉中所选项目的蓝色.我可以在点击某个项目但不在活动项目中时更改它.这是我使用的:在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>
Run Code Online (Sandbox Code Playgroud)
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="uva_color">#f2f8ee</color>
<color name="default_color">#f2f8ee</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在导航抽屉的布局:
<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"/>
Run Code Online (Sandbox Code Playgroud)
那么,当前选择的项目不会改变颜色.任何想法?
使用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) { …
Run Code Online (Sandbox Code Playgroud) 我正在我的应用程序上使用Nav抽屉.我已经制作了导航抽屉#000000(黑色)的背景.每当我选择任何项目时,文本的文本颜色从#E44F50(胡萝卜红色)变为黑色但我无法更改所选项目的背景颜色.我想要的是将所选项目的背景颜色从黑色更改为胡萝卜红色.
这是我的应用程序的github链接 -
https://github.com/manmeet-22/NewsSnips-app.git
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
app:headerLayout="@layout/header"
app:itemIconTint="@drawable/drawer_item" //changing text color
app:itemTextAppearance="@style/TextAppearance20"
app:itemTextColor="@color/drawer_item"
app:itemBackground="@drawable/drawer_selected_item" //trying to change backgroundcolor the same way
app:menu="@menu/drawer"
tools:context="com.androidexample.newssnips.app.NavigationDrawerFragment"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
在这个我尝试改变背景颜色,就像我为文本做的,但我的应用程序崩溃.
这是我的drawer_selected_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_selected="true"/>
<item android:color="@android:color/transparent"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是我的drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true" />
<item android:color="@color/colorAccent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这是我的MainActivity.java:
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null);
Menu menu = navigationView.getMenu();
//For chnaging the textColor and textSize of group's Name ie. Category
MenuItem categoryName= menu.findItem(R.id.categoryGroup);
SpannableString …
Run Code Online (Sandbox Code Playgroud) 我为我的应用程序创建了另一个主题,即Dark.我使用导航栏定义为
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
Run Code Online (Sandbox Code Playgroud)
它在菜单中定义了两组可选择的组
<item
android:id="@+id/sort_item"
android:title="One">
<menu>
<group
android:id="@+id/menu_group_sort"
android:checkableBehavior="single">
<item
android:id="@+id/nav_sort_new"
android:title="@string/menu_new" />
<item
android:id="@+id/nav_sort_hot"
android:title="@string/menu_hot" />
<item
android:id="@+id/nav_sort_top"
android:title="@string/menu_top" />
</group>
</menu>
</item>
<item
android:id="@+id/filter_item"
android:title="Two">
<menu>
<group
android:id="@+id/menu_group_filter"
android:checkableBehavior="single">
<item
android:id="@+id/nav_category_all"
android:title="@string/menu_all" />
<item
android:id="@+id/nav_category_business"
android:title="@string/menu_business" />
<item
android:id="@+id/nav_category_technology"
android:title="@string/menu_technology" />
<item
android:id="@+id/nav_category_politics"
android:title="@string/menu_politics" />
</group>
</menu>
</item>
Run Code Online (Sandbox Code Playgroud)
通常,所选项目的文本是colorPrimary,背景看起来是某种Android灰色.当我定义我的新主题时,我会这样使用它
<!-- Dark theme. -->
<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryInverse</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkInverse</item>
<item name="colorAccent">@color/colorAccentInverse</item>
<item name="android:textColor">@color/textColorPrimaryInverse</item>
<item name="android:textColorPrimary">@color/textColorPrimaryInverse</item>
<item name="android:textColorSecondary">@color/textColorSecondaryInverse</item>
<item …
Run Code Online (Sandbox Code Playgroud)