Android ActionBar列表导航:标题和弹出菜单中的不同文本颜色
我的操作栏中有一个黑色背景的导航列表。但是,弹出菜单有白色背景 因此,我想要实现的是,操作栏中的项目文本颜色为白色,而菜单弹出窗口中的项目文本颜色为黑色 这是迄今为止我得到的两个例子: 它应该是这样的: 有人知道解决办法吗 这是我的列表导航代码:Android ActionBar列表导航:标题和弹出菜单中的不同文本颜色,android,android-actionbar,actionbarsherlock,Android,Android Actionbar,Actionbarsherlock,我的操作栏中有一个黑色背景的导航列表。但是,弹出菜单有白色背景 因此,我想要实现的是,操作栏中的项目文本颜色为白色,而菜单弹出窗口中的项目文本颜色为黑色 这是迄今为止我得到的两个例子: 它应该是这样的: 有人知道解决办法吗 这是我的列表导航代码: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1lin
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, new String[] { "Item 1", "Item 2" });
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter,
new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return true;
}
});
getSupportActionBar().setSelectedNavigationItem(0)
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_下拉列表_item_1line,新字符串[]{“item 1”,“item 2”});
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION\u MODE\u列表);
getSupportActionBar().setListNavigationCallbacks(适配器、,
新的ActionBar.OnNavigationListener(){
@凌驾
公共布尔值onNavigationItemSelected(int-itemPosition,long-itemId){
返回true;
}
});
getSupportActionBar().setSelectedNavigationItem(0)
这是我使用过的一系列样式
<style name="CustomTheme" parent="@style/Theme.customized">
<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="actionDropDownStyle">@style/CustomSherlockDropDownNav</item>
<item name="android:actionDropDownStyle">@style/CustomSherlockDropDownNav</item>
<!-- didn't work: http://stackoverflow.com/questions/12395381/android-actionbar-navigation-spinner-text-color
<item name="android:spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
<item name="spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
-->
<!-- didn't work: http://stackoverflow.com/questions/11479186/styling-actionbar-dropdown-menu
<item name="android:actionBarWidgetTheme">@style/custom.actionBarWidgetTheme</item>
<item name="actionBarWidgetTheme">@style/custom.actionBarWidgetTheme</item>
-->
<!-- didn't work: http://android-developers.blogspot.de/2011/04/customizing-action-bar.html
<item name="android:dropDownListViewStyle">@style/CustomDropDownListView</item>
<item name="dropDownListViewStyle">@style/CustomDropDownListView</item>
-->
....
</style>
<style name="custom.actionBarWidgetTheme" parent="@style/Theme.Sherlock.Light.DarkActionBar">
<item name="android:spinnerDropDownItemStyle">@style/custom.Widget.DropDownItem.Spinner</item>
</style>
<style name="custom.Widget.DropDownItem.Spinner" parent="@style/Widget.Sherlock.DropDownItem.Spinner">
<item name="android:textAppearance">@style/custom.TextAppearance.Widget.DropDownItem</item>
</style>
<style name="custom.TextAppearance.Widget.DropDownItem" parent="@style/TextAppearance.Sherlock.Widget.DropDownItem">
<item name="android:textColor">#00A000</item>
</style>
<style name="CustomDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
<item name="android:textColor">#00A000</item>
<item name="android:textSize">8dip</item>
</style>
<style name="CustomSherlockDropDownNav" parent="@style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_customtab</item>
<item name="android:background">@drawable/spinner_background_ab_customtab</item>
</style>
@style/Widget.Styled.ActionBar
@style/Widget.Styled.ActionBar
@样式/自定义SherlockDropDownNav
@样式/自定义SherlockDropDownNav
....
@style/custom.Widget.DropDownItem.Spinner
@style/custom.TextAppearance.Widget.DropDownItem
#00A000
#00A000
8dip
@可绘图/菜单\下拉菜单\面板\自定义选项卡
@可绘制/微调器\u背景\u ab\u自定义选项卡
然而,没有什么不起作用 问题在于,对于微调器项和微调器下拉项,您使用的是相同的资源
android.R.layout.simple\u dropdown\u item\u 1line
使用R.layout.sherlock\u spinner\u项目
和R.layout.sherlock\u spinner\u下拉列表\u项目
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.sherlock_spinner_item, new String[] { "Item 1", "Item 2" });
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
ArrayAdapter=新的ArrayAdapter(此,
R.layout.sherlock_spinner_item,新字符串[]{“item 1”,“item 2”});
adapter.setDropDownViewResource(R.layout.sherlock\u微调器\u下拉菜单\u项);
这样,像
Widget.Sherlock.TextView.SpinnerItem这样的样式将起作用。您只需创建自定义xml即可实现这一点,它将成为您的列表项。按如下方式创建xml:
自定义列表项目.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:textSize="18sp"
android:textColor="@color/holo_dark_red"
android:paddingRight="110dip"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:textIsSelectable="false"
android:ellipsize="marquee" />
这应该可以做到(至少在我的应用程序中是这样)。
@样式/下拉导航.Item.Inverse
@style/ActionBar.TitleText
无衬线灯
@颜色/白色
应该是这样的。不知道为什么要使用微调器“项”样式。@@这非常有效,尤其是对于标准布局不足的情况,这无疑是一个有价值的提示。不过,我对这里预定义的布局很满意。谢谢你的回复!对于那些不想处理样式文件的人来说,这是一个很好的答案!这为我指明了正确的方向。我不知道正确的布局和(因此)应用的样式。我最终使用上面的布局运行了它,并将spinnerItemStyle
和spinnerDropDownItemStyle
添加到主主题中。文本颜色和大小的实际更改可以在派生的textpearance
样式中完成。非常感谢你的帮助!你是怎么做到的?你能发布一个工作代码吗?@user2247689:目前我无法访问源代码。基本上,我是从matthias的建议开始的:应用两种不同的布局并为它们设置自定义样式(覆盖textAppearance
;参见我的评论)。我建议您看看ActionBarSherlock,因为您可以快速查找链接样式源。后来我将我的实现更改为Android Developer的解决方案,因为我的应用程序中有多个微调器。。。我开始工作了:)我正在使用ActionBarSherlock(见上面的标签)。同时,我按照Android开发者的建议实现了一个自定义视图。谢谢你的回答:)希望你能从AppCompat主题样式中得到一些提示别忘了使用两种自定义布局。
adapter.setDropDownViewResource(R.layout.custom_list_item);
<style name="Theme.WhyCheck" parent="@style/Theme.AppCompat.Light">
<item name="android:spinnerItemStyle">@style/DropDownNav.Item.Inverse</item>
</style>
<style name="DropDownNav.Item.Inverse" parent="@style/Widget.AppCompat.DropDownItem.Spinner">
<item name="android:textAppearance">@style/ActionBar.TitleText</item>
</style>
<style name="ActionBar.TitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textColor">@color/white</item>
</style>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflating the layout for the custom Spinner
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom, null);
// Declaring and Typecasting the textview in the inflated layout
TextView tvLanguage = (TextView) layout
.findViewById(R.id.tvLanguage);
// Setting the text using the array
tvLanguage.setText(obj[position]);
//tvLanguage.setTextColor(Color.BLACK);
tvLanguage.setTextSize(14f);
return layout;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customdropdown, null);
// Declaring and Typecasting the textview in the inflated layout
TextView tvLanguage = (TextView) layout
.findViewById(R.id.tvLanguage);
// Setting the text using the array
tvLanguage.setText(obj[position]);
//tvLanguage.setTextColor(Color.BLACK);
tvLanguage.setTextSize(14f);
return layout;
}
}