答案 0 :(得分:33)
更改Dialog
标题分隔符颜色的唯一方法是通过继承Dialog
并使用Resources.getIdentifier
来查找标题分隔符View
。之后,您只需拨打View.setBackgroundColor
。由于这是自定义标题分隔符的唯一方法,因此您也可以继续使用相同的方法来自定义标题颜色。
但至于为什么你无法得到你为你工作的答案,很难说。您没有包含任何代码或您尝试过的任何内容,因此很难确定您没有收到所需结果的原因。
以下是更改标题颜色和标题分隔颜色的示例:
/**
* A sublcass of {@link AlertDialog} used to customize the title and title
* divider colors
*/
public class CustomDialog extends AlertDialog {
/**
* Constructor for <code>CustomDialog</code>
*
* @param context The {@link Context} to use
*/
public CustomDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Resources res = getContext().getResources();
final int yellow = res.getColor(android.R.color.holo_orange_light);
// Title
final int titleId = res.getIdentifier("alertTitle", "id", "android");
final View title = findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title divider
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
<强>实施强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final CustomDialog customDialog = new CustomDialog(this);
customDialog.setTitle("Title");
customDialog.setMessage("Message");
customDialog.show();
}
使用DialogFragment
AlertDialog.Builder
public class CustomDialogFragment extends DialogFragment {
/**
* Empty constructor as per the {@link Fragment} docs
*/
public CustomDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Message")
.create();
}
@Override
public void onStart() {
super.onStart();
final Resources res = getResources();
final int yellow = res.getColor(android.R.color.holo_orange_light);
// Title
final int titleId = res.getIdentifier("alertTitle", "id", "android");
final View title = getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title divider
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
<强>实施强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new CustomDialogFragment().show(getFragmentManager(), "customDialogFragment");
}
<强>结果