Issue Displaying a Custom Alert Dialog
I'm having some trouble getting a custom alert dialog to display correctly. To introduce the problem, my app can switch between Theme.Holo.Light and Theme.Holo.Dark dynamically. When I display an alert with the default layout, the alert dialog inherits the theme of the activity with no issue. When I define a custom layout for the alert dialog (containing textviews/edittexts) they display fine when the app is set to Theme.Holo.Dark, however, when it is set to Theme.Holo.Light the text in the textviews are displayed as a very light gray, and the edittext borders and text are nearly invisible (white on white background). The only way I could somewhat fix this is by directly declaring the color of the text in the textviews in the custom dialog xml layout file for each text field. To use this method, however, i would 开发者_运维百科need to create separate layouts for both the holo.light and holo.dark themes. In addition to this, I cannot find any information on how to change the border color for an edittext field, which is also an issue. I've tried creating and specifying a theme in the AlertDialog.Builder however this never seems to apply the text styles and as I've said before, I cannot figure out how to change the border color for the edittext fields or make them specifically inherit from the Holo.Light theme. Relevant code is below and any help would be greatly appreciated.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="First Name" android:id="@+id/fname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/fname_input" android:layout_width="225sp" android:layout_height="wrap_content"></EditText>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Last Name" android:id="@+id/lname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/lname_input" android:layout_width="225sp" android:layout_height="wrap_content"></EditText>
</TableRow>
</TableLayout>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = getArguments().getString("text");
String title = getArguments().getString("title");
View layout_view = null;
int layout = getArguments().getInt("layout");
if(layout != -1){
LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
layout_view = inflater.inflate(layout, (ViewGroup)getActivity().findViewById(R.layout.main));
}
return new AlertDialog.Builder(getActivity()/*,AlertDialog.THEME_HOLO_DARK*/)
.setTitle(title)
.setMessage(text)
.setView(layout_view)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}
)
.create();
}
精彩评论