我有一个包含旋转器
的对话框
。当我在模拟器上运行该应用程序时,一切正常,但当我在设备上运行它时,spinner
文本变为白色,并且单击的spinner
背景为黑色。我不明白为什么也不知道如何解决它。这非常烦人,因为无法再阅读文本。
我已经在我的应用程序的其他地方实现了spinners
,并且一切正常。仅当微调器
位于对话框
中时才会出现此问题。
这是我的对话框
的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new MyOnClickListener(itemInfo));
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);
//Create spinner
ArrayList<String> listForSpinner = (ArrayList) currentUser.getListNames();
String[] spinnerList = listForSpinner.toArray(new String[listForSpinner.size()]);
final ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, spinnerList);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(getApplicationContext());
spinner.setAdapter(adp);
spinner.setPadding(50, 50, 50, 0);
spinner.setPopupBackgroundResource(R.color.white); //test that wasn't successful
builder.setView(spinner);
AlertDialog dialog = builder.create();
dialog.show();
非常感谢!
最佳答案
我为解决此问题所做的就是创建自定义布局。
我在代码中更改了 2 行:
final ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), R.layout.dialog_spinner_item, spinnerList);
adp.setDropDownViewResource(R.layout.dialog_spinner_dropdown_item);
并创建了 2 个布局文件:
dialog_spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:background="@color/white"
android:textSize="@dimen/text_size_title_text_view">
</TextView>
dialog_spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:background="@color/white"
</CheckedTextView>
关于java - 安卓 : Spinner background gets dark and text white when installed on device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974475/