项目中有个需求,就是输入框里面长按,会跳出一个输入dialog,而此dialog如下图,下面是是我手指按着item截图的图片
图一、
此dialog原始的是什么样子呢?我们不谈title的编辑文字上半部分,而说下半部分,下半部分输入法三个字为黑色,背景色为白色
现在项目需要背景图片全部替换为黑色图片,而字又是黑色,就会看不到字了,按下去红色的背景,而就能看到黑色的字了,
图二为原androoid的,可以根据图二来想象一下
遇到这种情况,进行看代码,这个dialog主要是源码AlertDialog,而这个AlertDialog里面主要的界面控制为AlertController这个类,看这个类的
如下方法:
private void createListView(final AlertController dialog) {
final RecycleListView listView = (RecycleListView)
mInflater.inflate(R.layout.select_dialog, null);
ListAdapter adapter;
Log.d("AlertController", "create list view");
if (mIsMultiChoice) {
if (mCursor == null) {
Log.d("AlertController", "IsMultiChoice true,cursor is null");
adapter = new ArrayAdapter<CharSequence>(
mContext, R.layout.select_dialog_multichoice, R.id.text1, mItems) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (mCheckedItems != null) {
boolean isItemChecked = mCheckedItems[position];
if (isItemChecked) {
listView.setItemChecked(position, true);
}
}
return view;
}
};
} else {
Log.d("AlertController", "IsMultiChoice true,cursor is not null");
adapter = new CursorAdapter(mContext, mCursor, false) {
private final int mLabelIndex;
private final int mIsCheckedIndex;
{
final Cursor cursor = getCursor();
mLabelIndex = cursor.getColumnIndexOrThrow(mLabelColumn);
mIsCheckedIndex = cursor.getColumnIndexOrThrow(mIsCheckedColumn);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
CheckedTextView text = (CheckedTextView) view.findViewById(R.id.text1);
text.setText(cursor.getString(mLabelIndex));
listView.setItemChecked(cursor.getPosition(),
cursor.getInt(mIsCheckedIndex) == 1);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.select_dialog_multichoice,
parent, false);
}
};
}
} else {
int layout = mIsSingleChoice
? R.layout.select_dialog_singlechoice : R.layout.select_dialog_item;
if(mIsSingleChoice)
{
Log.d("AlertController", "IsSingleChoice true");
}else
{
Log.d("AlertController", "IsSingleChoice false");
}
if (mCursor == null) {
Log.d("AlertController", "IsMultiChoice false,cursor is null");
adapter = (mAdapter != null) ? mAdapter
: new ArrayAdapter<CharSequence>(mContext, layout, R.id.text1, mItems);
if(mAdapter != null)
{
Log.d("AlertController", "Adapter != null");
}else
{
Log.d("AlertController", "Adapter == null");
}
if(layout == R.layout.select_dialog_item)
{
Log.d("AlertController", "layout == R.layout.select_dialog_item");
}else if(layout == R.layout.select_dialog_singlechoice)
{
Log.d("AlertController", "layout == R.layout.select_dialog_singlechoice");
}
} else {
Log.d("AlertController", "IsMultiChoice false,cursor is not null");
adapter = new SimpleCursorAdapter(mContext, layout,
mCursor, new String[]{mLabelColumn}, new int[]{R.id.text1});
}
}
打印出如下日志:
06-26 19:30:10.460 D/AlertController( 1749): create list view
06-26 19:30:10.460 D/AlertController( 1749): IsSingleChoice false
06-26 19:30:10.460 D/AlertController( 1749): IsMultiChoice false,cursor is null ,mAdapter=com.android.internal.view.menu.MenuBuilder$MenuAdapter
06-26 19:30:10.460 D/AlertController( 1749): Adapter != null
06-26 19:30:10.460 D/AlertController( 1749): layout == R.layout.select_dialog_item
06-26 19:41:42.750 D/AlertController( 1365): create list view
06-26 19:41:42.750 D/AlertController( 1365): IsSingleChoice true
上面我去除了日志代码,但是从日志来看mAdapter对象为com.android.internal.view.menu.MenuBuilder$MenuAdapter
因为上面一个图点击后,还会跳出如下的图
图二、原始androd的界面
图三、定制后的
好吧,我们来继续说,上面这个图,即由图二修改为图三效果,只需要去修改select_dialog_singlechoice.xml顺便也把select_dialog_multichoice.xml修改了,其实就是在里面多加一行
android:textColor="@android:color/white"
整个如下:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:gravity="center_vertical"
android:paddingLeft="12dip"
android:paddingRight="7dip"
android:checkMark="@android:drawable/btn_radio"
android:ellipsize="marquee"
/>
上面的图二到图三的效果就能实现,这两个layout是在上面的createListView函数中被提到,修改布局文件中的textColor为white就ok了就能看到上面图三的效果即黑底白字“谷歌拼音输入法”和“手写输入法”,右边还有个check小圆圈图片,但是统一的修改textColor的值在select_dialog_item.xml中却实现不了黑背景白色的字,为什么呢?再回到上面,我说的:mAdapter对象为com.android.internal.view.menu.MenuBuilder$MenuAdapter这个很重要,
进入MenuBuilder中,我们看到这个类中有如下定义:
static final int LAYOUT_RES_FOR_TYPE[] = new int[] {
com.android.internal.R.layout.icon_menu_layout,
com.android.internal.R.layout.expanded_menu_layout,
0,
};
// Order must be the same order as the TYPE_*
static final int ITEM_LAYOUT_RES_FOR_TYPE[] = new int[] {
com.android.internal.R.layout.icon_menu_item_layout,
com.android.internal.R.layout.list_menu_item_layout,
com.android.internal.R.layout.list_menu_item_layout,
};
好吧,我们把上面提到的进行修改,如下文件
M core/res/res/layout/icon_menu_item_layout.xml +1, -0 Side-by-Side Unified
M core/res/res/layout/list_menu_item_layout.xml +2, -0 Side-by-Side Unified
主要还是在文件中增加
android:textColor="@android:color/white"
好吧,为了全句统一,我顺便也把下面的改了,即
core/res/res/values/styles.xml
<style name="TextAppearance.Widget.TextView.PopupMenu">
<item name="android:textSize">22sp</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:textColorHint">?textColorHint</item>
</style>
即:
<item name="android:textColor">?textColorPrimaryDisableOnly</item>
修改为:
<item name="android:textColor">#ffffffff</item>
现在都ok了,你就可以看到你要的效果了,即黑底色白字