有
我面临改变工具栏/操作栏溢出菜单项的文本颜色的问题。我做了太多的谷歌搜索和阅读Stackoverflow的答案,但它无法解决我的问题
currenlt我将黑色变成溢出菜单项。
我想要的是什么 1.我想将菜单项黑色的颜色更改为白色。 2.我想更改溢出菜单的宽度以匹配父屏幕
我的代码:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
<item name="android:divider">@color/divider</item>
<item name="icon">@color/icons</item>
<item name="actionOverflowMenuStyle">@style/AppTheme.Base.PopupMenu</item>
</style>
<style name="AppTheme.Base.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@color/primary</item>
</style>
请帮我解决这个问题..
提前致谢
答案 0 :(得分:1)
最后我得到了将黑色转换为白色的代码
public boolean onPrepareOptionsMenu(Menu menu) {
//return super.onPrepareOptionsMenu(menu);
try {
for (int i = 0; i < menu.size(); i++) {
MenuItem mi = menu.getItem(i);
mi.setIcon(R.drawable.ic_action_new);
String title = mi.getTitle().toString();
Spannable spannable = new SpannableString(title);
spannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(spannable);
}
} catch (Exception ex) {
}
return true;
}
答案 1 :(得分:0)
更灵活但不太受欢迎的方式是使用PopupWindow
MainActivity.java 包com.chiragjn.stackovefflowing;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String TAG = "MainActivity.java";
String popUpContents[];
PopupWindow popupWindow;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
List<String> itemsList = new ArrayList<String>();
itemsList.add("Option 1");
itemsList.add("Option 2");
itemsList.add("Option 3");
itemsList.add("Option 4");
popUpContents = new String[itemsList.size()];
itemsList.toArray(popUpContents);
popupWindow = popupWindow();
}
public PopupWindow popupWindow() {
PopupWindow popupWindow = new PopupWindow(this);
ListView listView = new ListView(this);
listView.setAdapter(dogsAdapter(popUpContents));
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(listView);
return popupWindow;
}
private ArrayAdapter<String> dogsAdapter(String dogsArray[]) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dogsArray) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
String text = item;
TextView listItem = new TextView(MainActivity.this);
listItem.setText(text);
listItem.setTextSize(22);
listItem.setPadding(10, 10, 10, 10);
listItem.setTextColor(Color.WHITE);
switch(position)
{
case 0:
listItem.setBackgroundColor(Color.BLUE);
break;
case 1:
listItem.setBackgroundColor(Color.GREEN);
break;
case 2:
listItem.setBackgroundColor(Color.YELLOW);
break;
case 3:
listItem.setBackgroundColor(Color.RED);
break;
default:
break;
}
return listItem;
}
};
return adapter;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_options:
View v = findViewById(R.id.action_options);
popupWindow.showAsDropDown(v, 0, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
main.xml中
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.chiragjn.stackovefflowing.MainActivity" >
<item android:id="@+id/action_options"
android:icon="@android:drawable/ic_menu_more"
android:title="Overflow Menu"
app:showAsAction="always" />
</menu>
然后,您可以在ListView中充气您最喜欢的布局并实现ListView onItemClickListner()